Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Types

Types in XML Schema are either simple or complex. Simple types allow character data content, but no child elements or attributes. In our example, the number and status elements, as well as the hireDate attribute, have simple types. Complex types, on the other hand, allow child elements or attributes. In our example, the employee element has a complex type because it has child elements and an attribute.

Built-in Simple Types

The XML Schema recommendation defines 44 built-in simple types that represent commonly used data types. They consist of string types, numeric types (for example, float, decimal, integer), and date and time types (for example, dateTime, duration, time). There are also types that represent each of the attribute types in XML 1.0, such as ID, IDREF, and NMTOKEN. In our example, we used the built-in simple type integer for the number element, and the built-in type date for the attribute hireDate. The built-in types are listed in Table 1.

Table 1. Built-in Simple Types

anyURI

base64Binary

boolean

byte

date

dateTime

decimal

double

duration

ENTITIES

ENTITY

float

gDay

gMonth

gMonthDay

gYear

gYearMonth

hexBinary

ID

IDREF

IDREFS

int

integer

language

long

Name

NCName

negativeInteger

NMTOKEN

NMTOKENS

nonNegativeInteger

nonPositiveInteger

normalizedString

NOTATION

positiveInteger

QName

short

string

time

token

unsignedByte

unsignedInt

unsignedLong

unsignedShort


User-Derived Simple Types

If the built-in simple types don't quite meet your needs, you can derive your own types from the built-in types. In our example, we created a new type StatusType that restricted string by applying enumeration facets. Facets are a way of restricting the value values of a type, and there are 12 of them available in XML Schema. They can be used to restrict a value to specific list, to a range, or to match a regular expression. The 12 facets are listed in Table 2.

Table 2. Facets

enumeration

fractionDigits

length

maxExclusive

maxInclusive

maxLength

minExclusive

minInclusive

minLength

pattern

totalDigits

whiteSpace


Complex Types

Complex types allow elements to have child elements or attributes, or both. Complex types can have one of four content types:

  • Simple content, which means that they only have character data content; for example, <status effDate="2001-11-30">FT</status>. Simple content complex types are similar to simple types, except that they may have attributes.

  • Element-only content, which has only child elements like our employee example.

  • Mixed content, which allows both character data content and child elements. It is often used in free-form text.

  • Empty content, which allows no content at all; for example, <status value="FT"/>

When complex types have child elements, a content model is specified to indicate the names, order, and number of child elements that may appear. In our example, we specified a content model for EmployeeType that consisted of a group with the two child elements. The purpose of a sequence group is to indicate that the elements must appear in the order specified. Two other varieties of groups can be used in a content model. A choice group may be used to indicate that any one of the elements may appear, and an all group may be used to indicate that all of the elements should appear, but in any order. Sequence and choice groups may be nested to create more complex content models. Example 4 shows a new definition for EmployeeType, which requires that the element contain first the number and status children, optionally followed by up to five location or favoriteColor elements, followed by any element from another namespace (indicated by the any element).

Example 4. Nested Groups

<xsd:complexType name="EmployeeType">
 <xsd:sequence>
  <xsd:element name="number" type="xsd:integer"/>
  <xsd:element name="status" type="StatusType"/>
  <xsd:choice minOccurs="0" maxOccurs="5">
   <xsd:element name="location" type="xsd:string"/>
   <xsd:element name="favoriteColor" type="ColorType"/>
  </xsd:choice>
  <xsd:any namespace="##other"/>
 </xsd:sequence>
 <xsd:attribute name="hireDate" type="xsd:date"/>
</xsd:complexType>
  • + Share This
  • 🔖 Save To Your Account