XML technologies / XML Schema / Regular expressions for specifying simple datatypes / Back to our library
(Excerpt from "XML Schema" by Eric van der Vlist)
Let us see where this Swiss Army knife helps in our library. The first datatype we promised to improve at the end of the previous chapter was the ISBN. Without fully addressing its structural details, which W3C XML Schema cannot fully validate, we can ensure exactly ten characters, all digits or the letter “X”:
<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>You may wonder why we retained xs:length, since it restricts validation less than the added xs:pattern . That is a fair question without a complete answer yet. Applications using the PSVI as metadata may or may not infer a string-length limit from a pattern. Retaining redundant facets may prove useful for providing extra information to such future applications.
W3C XML Schema cannot express that a book ID equals its ISBN prefixed with “b”. We can at least require a “b”, nine digits, then a digit or “X”:
<xs:simpleType name="bookID">
<xs:restriction base="xs:ID">
<xs:pattern value="b[0-9]{9}[0-9X]"/>
</xs:restriction>
</xs:simpleType>To use this datatype, remember that we currently have a global attribute referenced by element book but also used in elements character and author, which have different formats. This is the main restriction of global elements and attributes: they can be referenced only when their type is identical everywhere. We can work around this with a local definition on element book for attribute id using the new datatype.
Finally, we want to restrict dates, which need no timezones. Timezone information could actually cause problems when comparing them.
<xs:simpleType name="date">
<xs:restriction base="xs:date">
<xs:pattern value="[^:Z]*"/>
</xs:restriction>
</xs:simpleType>
Our new 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: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="author">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<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>
<xs:element name="character">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
</xs:schema>
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "XML Schema" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top