XML technologies / XML Schema / Our first schema / The first schema
(Excerpt from “XML Schema” by Eric van der Vlist)
Throughout this book, we shall see many different schema-writing styles, and even more approaches to deriving a schema from an instance document. For our first schema, we adopt a style familiar to those who have worked with DTDs. We begin by compiling a classified list of the elements and attributes to be found in the schema.
The elements in our instance document are named author, book, born, character, dead, isbn, library, name, qualification and title, while available, id and lang appear as attributes.
We construct this first schema by defining each element in turn below the schema document element named schema, which belongs to the namespace W3C XML Schema ("http://www.w3.org/2001/XMLSchema"), and usually carries the prefix “xs”.
Before starting, we must classify the elements. For this exercise, we need some key definitions to understand how W3C XML Schema performs this classification. These definitions will be developed in more detail on the pages about simple and complex types.
The content model characterizes the kinds of child elements and text nodes an element can contain, disregarding attributes.
The content model is “empty” when neither child elements nor text nodes are expected, “simple” when only text nodes are allowed, “complex” when only subelements are expected, and “mixed” when both are possible. Only elements and text nodes count when determining the model; attributes, comments and processing instructions are ignored. For example, an element with some attributes, a comment and several processing instructions has an empty content model if it has no text or element children.
Elements such as name, born and title have simple content models:
.../...
<title lang="de">
Auf den Hund gekommen
</title>
.../...
<name>
Charles M. Schulz
</name>
<born>
1922-11-26
</born>
.../...
Elements such as library or character have complex content models:
<library>
<book id="b3810518883" available="true">
.../...
</book>
</library>
<character id="Lucy">
<name>
Lucy
</name>
<born>
1952-03-03
</born>
<qualification>
herrschsüchtig, kratzbürstig und egoistisch
</qualification>
</character>Among elements with simple content models, we can distinguish those with attributes from those that cannot carry attributes. Later chapters show how W3C XML Schema can also represent simple and mixed content models.
W3C XML Schema considers elements with a simple content model and no attributes to be “simple types”; all other elements, including simple content with attributes and other content models, are “complex types”. In other words, an element accepting only text nodes, without child elements or attributes, is considered a simple type; every other case is a complex type.
Attributes are always of simple type because they have no children and can contain only a text node.
In our example, elements such as author or title have a complex type:
<author id="CMS">
<name>
Charles M. Schulz
</name>
<born>
1922-11-26
</born>
<dead>
2000-02-12
</dead>
</author>
.../...
<title lang="de">
Auf den Hund gekommen
</title>
In contrast, elements such as born or qualification, and of course all attributes, have a simple type:
<born>
1922-11-26
</born>
.../...
<qualification>
brachte die klassische Musik in die Peanuts-Comics ein
</qualification>
.../...
<book available="true"/>Now that we have criteria for classifying our components, we can define them all. Let us start with the simplest by choosing an element of simple type, such as the element name, which can occur within author or character:
<name>
Charles M. Schulz
</name>To define such an element, we use an xs:element , global definition, placed directly below the document element xs:schema :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:string"/>
.../...
</xs:schema>The value used to reference the data type, xs:string, carries the prefix xs, the prefix associated with W3C XML Schema. This indicates that xs:string is a data type predefined by W3C XML Schema.
The same can be done for all other simple types and for attributes:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:string"/>
<xs:element name="qualification" type="xs:string"/>
<xs:element name="born" type="xs:date"/>
<xs:element name="dead" type="xs:date"/>
<xs:element name="isbn" type="xs:string"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="xs:language"/>
.../...
</xs:schema>Although this design style should be familiar to DTD users, it is flatter than a DTD because attributes are declared outside element declarations. This produces a schema treating elements and attributes quite similarly. However, we shall see that this simple flat style is usually impractical for a schema describing an XML vocabulary using namespaces.
Treating simple-type elements and attributes alike simplifies the data models of XPath, DOM and Infoset. They consider a simple-type element an entity with a single child of type “character”, but an attribute an entity with a normalized value. The benefit of this simplification is that we can use simple data types to define and write simple-type elements and attributes consistently, without distinction:
<xs:element name="isbn" type="xs:string"/>
or
<xs:attribute name="isbn" type="xs:string"/>Definition order in a schema is immaterial. Let us now move towards greater type complexity and define the element title, which appears in the instance document as follows:
<title lang="de">
Auf den Hund gekommen
</title>Because this element has an attribute, its type is complex. Since it contains only a text node, its content is simple. We therefore write its definition as follows:
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>In XML syntax, this looks rather verbose, but can almost be read as plain language: “The element named title has a complex type with simple content, obtained by extending the predefined data type xs:string by adding the attribute named lang defined in this schema.”
The remaining elements, library , book , author and character, all have complex types with complex content. They are defined by specifying their sequence of elements and attributes.
The element library, the simplest of these elements, is defined as:
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>This definition can be read: “The element named library is a complex type consisting of a sequence of one to arbitrarily many occurrences, note the attribute maxOccurs, of elements defined under the name book .”
The element author, which has an attribute and whose date of death can be considered optional, could look like this:
<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>This means that the element named author is a complex type consisting of a sequence of three elements, name , born and dead , and the attribute id . The element dead is optional: zero occurrences are allowed.
The attributes minOccurs and maxOccurs seen in some previous elements specify the minimum and maximum number of occurrences. Both default to 1, so when both are absent the element must appear exactly once in the sequence. The special value “unbounded” can be used for maxOccurs when the number of occurrences has no upper limit.
Attributes must be defined after the sequence. The remaining elements, book and character, can be defined in the same way, giving the following complete schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:string"/>
<xs:element name="qualification" type="xs:string"/>
<xs:element name="born" type="xs:date"/>
<xs:element name="dead" type="xs:date"/>
<xs:element name="isbn" type="xs:string"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<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 ref="id"/>
<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 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
back to top