Using W3C XML Schema
by Eric van der Vlist
|
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9
Groups, Compositors and Derivation
Groups
W3C XML Schema also allows the definition of groups of elements and attributes.
<!-- definition of an element group -->
<xs:group name="mainBookElements">
<xs:sequence>
<xs:element name="title" type="nameType"/>
<xs:element name="author" type="nameType"/>
</xs:sequence>
</xs:group>
<!-- definition of an attribute group -->
<xs:attributeGroup name="bookAttributes">
<xs:attribute name="isbn" type="isbnType" use="required"/>
<xs:attribute name="available" type="xs:string"/>
</xs:attributeGroup>
These groups can be used in the definition of complex types, as shown below.
<xs:complexType name="bookType">
<xs:sequence>
<xs:group ref="mainBookElements"/>
<xs:element name="character" type="characterType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attributeGroup ref="bookAttributes"/>
</xs:complexType>
These groups are not datatypes but containers holding a set of elements or attributes that can be used to describe complex types.
Compositors
So far, we have seen the xs:sequence compositor which defines
ordered groups of elements (in fact, it defines ordered group of particles, which
can also be groups or other compositors). W3C XML Schema supports two
additional compositors that can be mixed to allow various combinations. Each of
these compositors can have minOccurs and maxOccurs
attributes to define their cardinality.
The xs:choice compositor describes a choice between several possible elements or groups of elements.
The following group --compositors can appear within groups, complex types or other compositors-- will accept either a single name element or a sequence of firstName,
an optional middleName and a lastName:
<xs:group name="nameTypes">
<xs:choice>
<xs:element name="name" type="xs:string"/>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="middleName" type="xs:string" minOccurs="0"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
</xs:choice>
</xs:group>
The xs:all compositor defines an unordered set of elements. The following complex type definition allows its contained elements to appear in any order:
<xs:complexType name="bookType">
<xs:all>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="character" type="characterType" minOccurs="0"
maxOccurs="unbounded"/>
</xs:all>
<xs:attribute name="isbn" type="isbnType" use="required"/>
</xs:complexType>
In order to avoid combinations that could become ambiguous or too complex to
be solved by W3C XML Schema tools, a set of restrictions has been added to the
xs:all particle:
- they can appear only as a unique child at the top of a content model
- and their children can be only xs:element definitions or references and cannot have a cardinality greater than one.
Derivation of simple types
Simple datatypes are defined by derivation of other datatypes, either predefined and identified by the W3C XML Schema namespace or defined elsewhere in your schema.
We have already seen examples of simple types derived by restriction (using
xs:restriction elements). The different kind of restrictions that can be
applied on a datatype are called facets. Beyond the xs:pattern
(using a regular expression syntax) and xs:maxLength facets shown
already, many facets allow constraints on the length of a value, an enumeration
of the possible values, the minimal and maximal values, its precision and
scale, etc.
Two other derivation methods are available that allow to define white space separated lists and
union of datatypes. The following definition uses xs:union extends the definition of our type for isbn to accept the values
TDB and NA:
<xs:simpleType name="isbnType">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{10}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="TBD"/>
<xs:enumeration value="NA"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
The union has been applied on the two embedded simple types to
allow values from both datatypes, our new datatype will now accept the values
from an enumeration with two possible values (TBD and NA).
The following example type (isbnTypes) uses xs:list to define a
whitespace-separated list of ISBN values. It also derives a type (isbnTypes10)
using xs:restriction that accept between 1 and 10 values,
separated by a whitespace:
<xs:simpleType name="isbnTypes">
<xs:list itemType="isbnType"/>
</xs:simpleType>
<xs:simpleType name="isbnTypes10">
<xs:restriction base="isbnTypes">
<xs:minLength value="1"/>
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>