Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

8.2. Simple type definitions

8.2.1. Named simple types

Simple types can be either named or anonymous. Named simple types are always defined globally (i.e., their parent is always schema 1) and are required to have a name that is unique among the types (both simple and complex) in the schema. The syntax for a named simple type definition is shown in Table 8-1.

Table 8-1. XSD Syntax: named simple type definition

Name

simpleType

Parents

schema, redefine, 1.1override

Attribute name

Type

Description

id

ID

Unique ID.

name

NCName

Simple type name.

final

"#all" | list of ("restriction" | "list" | "union" | 1.1"extension")

Whether other types can be derived from this one (see Section 8.5); defaults to finalDefault of schema.

Content

annotation?, (restriction | list | union)

The name of a simple type must be an XML non-colonized name, which means that it must start with a letter or underscore, and may only contain letters, digits, underscores, hyphens, and periods. You cannot include a namespace prefix when defining the type; it takes its namespace from the target namespace of the schema document.

All examples of named types in this book have the word “Type” at the end of their names to clearly distinguish them from element and attribute names. However, this is a convention and not a requirement. You can even have a type definition and an element declaration using the same name, but this is not recommended because it can be confusing.

Example 8–1 shows the definition of a named simple type DressSizeType along with an element declaration that references it. Named types can be used in multiple element and attribute declarations.

Example 8–1. Defining and referencing a named simple type

<xs:simpleType name="DressSizeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="2"/>
    <xs:maxInclusive value="18"/>
  </xs:restriction>
</xs:simpleType>

<xs:element name="size" type="DressSizeType"/>

8.2.2. Anonymous simple types

Anonymous types, on the other hand, must not have names. They are always defined entirely within an element or attribute declaration, and may only be used once, by that declaration. Defining a type anonymously prevents it from ever being restricted, used in a list or union, redefined, or overridden. The syntax to define an anonymous simple type is shown in Table 8-2.

Table 8-2. XSD Syntax: anonymous simple type definition

Name

simpleType

Parents

element, attribute, restriction, list, union, 1.1alternative

Attribute name

Type

Description

id

ID

Unique ID.

Content

annotation?, (restriction | list | union)

Example 8–2 shows the definition of an anonymous simple type within an element declaration.

Example 8–2. Defining an anonymous simple type

<xs:element name="size">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

8.2.3. Design hint: Should I use named or anonymous types?

The advantage of named types is that they may be defined once and used many times. For example, you may define a type named ProductCodeType that lists all of the valid product codes in your organization. This type can then be used in many element and attribute declarations in many schemas. This has the advantages of

  • Encouraging consistency throughout the organization
  • Reducing the possibility of error
  • Requiring less time to define new schemas
  • Simplifying maintenance, because new product codes need only be added in one place

If a type is named, you can also derive new types from it, which is another way to promote reuse and consistency.

Named types can also make a schema more readable when its type definitions are complicated.

An anonymous type, on the other hand, can be used only in the element or attribute declaration that contains it. It can never be redefined, overridden, have types derived from it, or be used in a list or union type. This can seriously limit its reusability, extensibility, and ability to change over time.

However, there are cases where anonymous types are preferable to named types. If the type is unlikely to ever be reused, the advantages listed above no longer apply. Also, there is such a thing as too much reuse. For example, if an element can contain the values 1 through 10, it does not make sense to define a type named OneToTenType to be reused by other unrelated element declarations with the same value space. If the value space for one of the element declarations using that named type changes but the other element declarations stay the same, it actually makes maintenance more difficult, because a new type would need to be defined at that time.

In addition, anonymous types can be more readable when they are relatively simple. It is sometimes desirable to have the definition of the type right there with the element or attribute declaration.

  • + Share This
  • 🔖 Save To Your Account