XML technologies / XML Schema / Creating complex data types / Simple content models
(Excerpt from “XML Schema” by Eric van der Vlist)
We begin with complex types containing simple content, closest to the simple types we know and an easier transition to complex content. We will not repeat simple-type creation and derivation covered in Creating simple data types, but focus on creating and deriving complex-type content models with only attributes and text nodes.
Creating simple content models
A complex type with simple content is created by adding attributes to a simple type, called extending the simple type. The notation is straightforward; examples appeared in Using predefined simple data types:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="string255">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>The only change needed is that the simple-type definition cannot be directly embedded in xs:extension, complex content, and must instead be referenced through attribute base.
The same notation and meaning can create global complex types for defining elements:
<xs:complexType name="tokenWithLang">
<xs:simpleContent>
<xs:extension base="xs:token">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="title" type="tokenWithLang"/>Deriving simple content
Complex types provide several ways to extend simple content models.
Derivation by extension
Extension is reserved for complex types, with no simple-type equivalent. It increases permitted or required child elements or attributes. Simple content cannot gain children, leaving only the same attribute extension used to create it from a simple type. To add an attribute to complex type tokenWithLang from the previous example, write:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="tokenWithLang">
<xs:attribute name="note" type="xs:token"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>Derivation by restriction
Restricting complex types with simple content sits at the boundary of the two parts of W3C XML Schema (Part 1: Structures, Part 2: Datatypes). It also closely resembles restriction of simple data types. The difference is that complex simple-content restriction constrains both text and attributes. The principle is the same: every instance valid under the restriction must be valid under the base type, except for the previously noted facet xs:whitespace.
Text-child restriction uses the same syntax and facets as simple types. Facets are followed by the new attribute list. Attribute types may differ if derived from the corresponding base attributes. Optional base attributes may be prohibited; unmentioned attributes remain unchanged. Examples begin with a simple-content type matching the previous model:
<xs:complexType name="tokenWithLangAndNote">
<xs:simpleContent>
<xs:extension base="xs:token">
<xs:attribute name="lang" type="xs:language"/>
<xs:attribute name="note" type="xs:token"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
First restrict text length as for simple types:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="tokenWithLangAndNote">
<xs:maxLength value="255"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:attribute name="note" type="xs:token"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
To remove attribute note from element title, declare note as prohibited in the restriction’s attribute list:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="tokenWithLangAndNote">
<xs:maxLength value="255"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:attribute name="note" use="prohibited"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>Attribute restrictions can also restrict the type. For example, restrict possible languages directly in the derived type’s definition of attribute lang:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="tokenWithLangAndNote">
<xs:maxLength value="255"/>
<xs:attribute name="lang">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:enumeration value="en"/>
<xs:enumeration value="de"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>Comparing the methods
Despite similarities, extension and restriction share little beyond deriving new simple-content types. Extension only adds attributes, without changing text or existing attribute types. Restriction is more flexible: it restricts text and attribute types and may remove optional base attributes.
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