XML technologies / XML Schema / Creating complex data types / Back to our library
(Excerpt from “XML Schema” by Eric van der Vlist)
This chapter contains so much material that the most useful possibilities are not obvious. The choice also depends on external factors, such as how fully the tools used support W3C XML Schema. Some tools generating Java classes or bindings may benefit from complex-type restriction. We will follow that path for now, creating a complex type with complex content that is a superset of the content models of author and character, which we will derive by restriction. First, we can define an empty content model with attribute id, from which all models also having this attribute can be derived by extension.
<xs:complexType name="elementWithID">
<xs:attribute ref="id"/>
</xs:complexType>
However, this type cannot directly define element book, because its attribute id is a restriction of xs:ID.
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="bookID"/>
<xs:attribute ref="available"/>
</xs:complexType>
</xs:element>To use complex type elementWithID to define element book, we must derive by extension a complex type corresponding to the complex type of book, but without the restriction on attribute id. The following code is verbose, but shown as an exercise:
<xs:complexType name="bookTmp">
<xs:complexContent>
<xs:extension base="elementWithID">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="available"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="book">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="bookTmp">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="bookID"/>
<xs:attribute ref="available"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>A more compact approach is to derive by restriction first:
<xs:complexType name="elementWithBookID">
<xs:complexContent>
<xs:restriction base="elementWithID">
<xs:attribute name="id" type="bookID"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="book">
<xs:complexContent>
<xs:extension base="elementWithBookID">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="available"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>It is easy, though verbose, to derive from elementWithID by extension a type personType , which can then derive elements author and character by restriction. We have already seen this example. The complete schema is:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="string255">
<xs:restriction base="xs:token">
<xs:maxLength value="255"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="string32">
<xs:restriction base="xs:token">
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="isbn">
<xs:restriction base="xs:NMTOKEN">
<xs:length value="10"/>
<xs:pattern value="[0-9]{9}[0-9X]"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="bookID">
<xs:restriction base="xs:ID">
<xs:pattern value="b[0-9]{9}[0-9X]"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="supportedLanguages">
<xs:restriction base="xs:language">
<xs:enumeration value="en"/>
<xs:enumeration value="de"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="date">
<xs:restriction base="xs:date">
<xs:pattern value="[^:Z]*"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="name" type="string32"/>
<xs:element name="qualification" type="string255"/>
<xs:element name="born" type="date"/>
<xs:element name="dead" type="date"/>
<xs:element name="isbn" type="isbn"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="supportedLanguages"/>
<xs:complexType name="elementWithID">
<xs:attribute ref="id"/>
</xs:complexType>
<xs:complexType name="bookTmp">
<xs:complexContent>
<xs:extension base="elementWithID">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="available"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="personType">
<xs:complexContent>
<xs:extension base="elementWithID">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
<xs:element ref="qualification" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="string255">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="bookTmp">
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="bookID"/>
<xs:attribute ref="available"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="author">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="personType">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="personType">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schemaDerivation or groups
Because complex-type derivation methods do not extend the range of structures definable with W3C XML Schema and are rather complicated, their use is debatable.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de