Using W3C XML Schema
by Eric van der Vlist
|
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9
Slicing the Schema
While the previous design method is very simple, it can lead to a depth in the embedded definitions, making it hardly readable and difficult to maintain when documents are complex. It also has the drawback of being very different from a DTD structure, an obstacle for human or machine agents wishing to transform DTDs into XML Schemas, or even just use the same design guides for both technologies.
The second design is based on a flat catalog of all the elements available in the instance document and, for each of them, lists of child elements and attributes. This effect is achieved through using references to element and attribute definitions that need to be within the scope of the referencer, leading to a flat design:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple type elements -->
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="friend-of" type="xs:string"/>
<xs:element name="since" type="xs:date"/>
<xs:element name="qualification" type="xs:string"/>
<!-- definition of attributes -->
<xs:attribute name="isbn" type="xs:string"/>
<!-- definition of complex type elements -->
<xs:element name="character">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="friend-of" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="since"/>
<xs:element ref="qualification"/>
<!-- the simple type elements are referenced using
the "ref" attribute -->
<!-- the definition of the cardinality is done
when the elements are referenced -->
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="author"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="isbn"/>
</xs:complexType>
</xs:element>
</xs:schema>
Download this schema: library2.xsd
Using a reference to an element or an attribute is somewhat comparable to cloning an object. The element or attribute is defined first, and it can be duplicated at another place in the document structure by the reference mechanism, in the same way an object can be cloned. The two elements (or attributes) are then two instances of the same class.
The next section shows how we can define such classes, called "types," that enables us to re-use element definitions.