XML technologies / XML Schema / Creating simple data types / Some simple-type oddities
(Excerpt from “XML Schema” by Eric van der Vlist)
Though structurally simple, simple types bring complications worth watching.
Watch the order
The order of restriction, list and union derivations matters greatly.
As seen, list and union derivations lose type semantics and facets, replacing them with common facets having specific meanings: xs:length, xs:maxLength, xs:minLength, xs:enumeration and xs:whitespace for list derivation; xs:pattern and xs:enumeration for union derivation.
All constraints on atomic or item types must therefore be applied before list or union derivation, as the facet sections explained. Afterwards, further restriction can use the common facet set.
The order of list and union derivation depends on the desired result: a list of unions differs from a union of lists.
<xs:simpleType name="listOfUnions">
<xs:list>
<xs:simpleType>
<xs:union memberTypes="xs:date xs:integer"/>
</xs:simpleType>
</xs:list>
</xs:simpleType>
<xs:simpleType name="UnionOfLists">
<xs:union>
<xs:simpleType>
<xs:list itemType="xs:date"/>
</xs:simpleType>
<xs:simpleType>
<xs:list itemType="xs:integer"/>
</xs:simpleType>
</xs:union>
</xs:simpleType>
These two data types match these fragments:
<UnionOfLists>
2001-01-01 2001-01-02
</UnionOfLists>
<UnionOfLists>
1 2 3
</UnionOfLists>
<ListOfUnions>
2001-01-01 2001-01-02
</ListOfUnions>
<ListOfUnions>
1 2 3
</ListOfUnions>
<ListOfUnions>
2001-01-01 1 2
</ListOfUnions>
The following does not match:
<UnionOfLists>
2001-01-01 1 2
</UnionOfLists>Here, every list item must have the same data type.
The order of successive restrictions also matters when redefining facets, since each facet has its own limits, as we saw.
Using or abusing lists to change length-facet behavior
We saw that list derivation affects not only item value spaces but also meanings, replacing their facets with a common set.
Before list derivation, length facets count characters normally or bytes for binary types. Afterwards, every list type counts values separated by whitespace.
Restricting the list then constrains the number of whitespace-separated words of any type. A string containing 100 to 200 words, each at most 15 letters and using only basic Latin characters, can be defined as:
<xs:simpleType name="word">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="15"/>
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
<xs:simpleType name="story">
<xs:restriction base="word">
<xs:minLength value="100"/>
<xs:maxLength value="200"/>
</xs:restriction>
</xs:simpleType>
The first definition restricts words; the second restricts the whole string as a word list. But this example cannot constrain the total character count in the story. The next chapter shows how patterns on the string itself express both restrictions.
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