XML technologies / XML Schema / Referencing schemas and schema data types in XML documents / Defining element types

(Excerpt from “XML Schema” by Eric van der Vlist)

The next hint we can give a schema processor helps it determine an element’s simple or complex type. A schema validator normally infers an element’s type from its name and its parent’s content model. The author of an instance document can override this inference with the attribute xsi:type, provided the new type derives by restriction or extension from the type specified in the schema document. Since an attribute in the instance document defines this type, this is possible only for elements. Attributes cannot have attributes!

Why would we want to define a type in the instance document? The answer differs slightly for simple and complex types, and also depends on whether our interest in schemas is limited to validation or includes data binding.

Defining simple types

An element or attribute can belong to several simple types. Derivation by union is generally a good way to let a schema validator choose the right type without using xsi:type. We can take this quite far. Having learned union derivation and patterns, let us illustrate them by defining a union type accepting ISO 8601 dates, a common English format, “April 2nd, 1998”, and the German format, “2. April 1998”. We can start by defining an ISO date without a time zone, as discussed in Using predefined simple data types:

<xs:simpleType name="dateISO">
   <xs:restriction base="xs:date">
      <xs:pattern value="[^:Z]*"/>
   </xs:restriction>
</xs:simpleType>

The English format can be described with different patterns for months of 31, 30 and 29 days. Our example does not handle non-leap years separately. The following definition should give a fairly good approximation for years AD with at most four digits. Lines are broken for readability; each pattern should occupy a single line:

<xs:simpleType name="EnglishDate">
   <xs:restriction base="xs:token">
      <xs:pattern value="(January|March|May|July|August|October|December) ([1-3]?1st|[12]?2nd|[12]?3rd|(30|[12]?[4-9])th),[0-9]{0,4}"/>
      <xs:pattern value="February ([1-2]?1st|[12]?2nd|[12]?3rd|[12]?[4-9]th),[0-9]{0,4}"/>
      <xs:pattern value="(April|June|September|November) ([1-2]?1st|[12]?2nd|[12]?3rd|(30|[12]?[4-9])th),[0-9]{0,4}"/>
   </xs:restriction>
</xs:simpleType>

After the English format, the German one looks easy. The same principle applies; line breaks have again been added for readability:

<xs:simpleType name="deutschesDatum">
   <xs:restrictionbase="xs:token">
      <xs:pattern value="([1-3][01]|[12]?[1-9])\. (Januar|März|Mai|Juli|August|Oktober|Dezember)\d{0,4}"/>
      <xs:pattern value="([12][01]|[12]?[1-9])\. Februar \d{0,4}"/>
      <xs:pattern value="([12][01]|[12]?[1-9]|30) (April|Juni|September|November)\d{0,4}"/>
      <xs:restriction>
</xs:simpleType>

Finally, derive the type by union as follows:

<xs:simpleType name="anydate">
  <xs:union memberTypes="dateISO EnglishDate deutschesDatum"/>
</xs:simpleType>

We now have a simple type accepting three date formats. A schema processor should validate all three and indicate in the PSVI which type it recognized. We achieved this without adding anything to the instance document. Why supply such information there at all? There are good reasons. First, applications unable to obtain it from the PSVI should also have access. This is common with current tools because no interface specification explains how an application can read the PSVI. That does not matter for validation alone, but may matter if we do not want applications processing our documents to have to determine the format themselves.

The second reason is not visible in the previous example. Its member types have nonoverlapping lexical spaces, preventing ambiguity, but that is not always so. We may wish to override the validator’s choice, or even use a generic, universal type in the schema and rely on instance documents to specify the type used. Protocol or binding applications, for which XML is merely a temporary serialization format, are good candidates. They often need generic elements capable of holding parameters of any type.

For example, a schema-based XML-RPC can be defined by the first example in the XML-RPC specification:

<methodCall>
   <methodName>
      examples.getStateName
   </methodName>
   <params>
      <param>
         <value>
            <i4>
               41
            </i4>
         </value>
      </param>
   </params>
</methodCall>

In a hypothetical W3C XML Schema-aware version of XML-RPC, this could be replaced by:

<methodCall xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <methodName>
      examples.getStateName
   </methodName>
   <params>
      <param>
         <value xsi:type="xs:int">
            41
         </value>
      </param>
   </params>
</methodCall>

Defining complex types

Although forcing a complex type in an instance document works much like it does for simple types, the motives can be quite different. This construction can still provide a polymorphic accessor, in the W3C Protocol Working Group’s terminology, but complex types rarely need it for that purpose because they lack derivation by union. The equivalent of simple-type union would be defining several content models for one element, having the processor try each, and retaining the first matching the instance fragment. That would be convenient, but is precisely what consistent declaration and unique particle attribution rules explicitly prohibit. Thus, within the schema itself, complex types offer no alternative to xsi:type. However, this construction often works around those rules.

Alternatively, think of this construction as a hint allowing the processor to resolve an available choice unambiguously and avoid breaking a rule. A typical use circumvents unique particle attribution to allow two content models for one element. In Defining uniqueness, keys and key references, we saw how xs:key could allow a title to be either an attribute or an element. This workaround does not support more complex combinations, such as either a title attribute or several title elements:

<book id="b3810518883" available="true" title="Auf den Hund gekommen">
   .../...
</book>

or:

<book id="b3810518883" available="true" type="bookTitleElements">
   <isbn>
      3810518883
   </isbn>
   <title lang="de">
      Auf den Hund gekommen
   </title>
   <title lang="en">
      Being a Dog Is a Full-Time Job
   </title>
   .../...
</book>

We define a base type that is a superset of both content models:

<xs:complexType name="bookBase">
   <xs:sequence>
      <xs:element ref="isbn"/>
      <xs:element ref="title" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
   </xs:sequence>
   <xs:attribute ref="id"/>
   <xs:attribute ref="title"/>
   <xs:attribute ref="available"/>
</xs:complexType>

It accepts book elements with optional titles expressed as attributes or elements. By restriction, we can derive a first type accepting only title attributes:

<xs:complexType name="bookTitleAttribute">
   <xs:complexContent>
      <xs:restriction base="bookBase">
         <xs:sequence>
            <xs:element ref="isbn"/>
            <xs:element ref="author" minOccurs="0"
               maxOccurs="unbounded"/>
            <xs:element ref="character" minOccurs="0"
               maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:restriction>
   </xs:complexContent>
</xs:complexType>

We can derive another type accepting only titles defined as one or more elements:

<xs:complexType name="bookTitleElements">
   <xs:complexContent>
      <xs:restriction base="bookBase">
         <xs:sequence>
            <xs:element ref="isbn"/>
            <xs:element ref="title" minOccurs="1" maxOccurs="unbounded"/>
            <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
         </xs:sequence>
         <xs:attribute ref="title" use="prohibited"/>
      </xs:restriction>
   </xs:complexContent>
</xs:complexType>

With these building blocks assembled, we use them in the schema to define the book element with type bookBase:

<xs:element name="book" type="bookBase"/>

Instance documents can then declare which derived type they use:

<book id="b3810518883" available="true" xsi:type="bookTitleElements">
   <isbn>
      3810518883
   </isbn>
   <title lang="de">
      Auf den Hund gekommen
   </title>
   <title lang="en">
      Being a Dog Is a Full-Time Job
   </title>
   .../...
</book>

or:

<book id="b3810518883" available="true" title="Auf den Hund gekommen" xsi:type="bookTitleAttribute">
   .../...
</book>

This still allows instance documents to use the base type, which may be undesirable: they could contain no title at all, or an attribute together with one or more elements, which we want to avoid. Defining the base type as “abstract” prohibits its use. Setting this attribute in the complex-type definition prevents instance documents from using the type; instead they must specify a derived type with the attribute xsi:type.

The attribute abstract mirrors the attribute block, already familiar to us. The latter prevents further derivation; the former requires it. The final definition of our complex base type is therefore:

<xs:complexType name="bookBase" abstract="true">
   <xs:sequence>
      <xs:element ref="isbn"/>
      <xs:element ref="title" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
   </xs:sequence>
   <xs:attribute ref="id"/>
   <xs:attribute ref="title"/>
   <xs:attribute ref="available"/>
</xs:complexType>

   

 

 

 

back to top