|
The XSD schema is defined using the following header statements:
|
|
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
|
Note the use of the xmlns:xsd attribute, which declares the namespace. This value is carried forward throughout the schema by the use of the xsd: prefix. |
|
Schemas can be fully customized, and developers can create and link schemas to their own namespaces, by using custom prefixes.
|
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
|
|
XML schema elements have either simple or complex types, depending on whether or not they contain attributes or child elements. Since book is a parent element that will have child elements, it has a complex type. The sequence element acts as a compositor, setting up the sequence of the nested child elements.
|
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
</xsd:sequence>
|
|
Since neither of the title or author elements have attributes or child elements, they have simple types, namely the built-in type xsd:string.
The book element, however, does have an attribute, which is declared separately within the complexType construct.
|
<xsd:attribute name="category">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Fiction"/>
<xsd:enumeration value="Non-Fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
|
|
Note that the use of this attribute also introduces the schema’s first validation rule. The category attribute is limited to one of two values, through the use of the restriction element. Also, notice the use of the simpleType element – since XML attributes cannot contain further attributes or child elements, they always have simple types.
|
|
The declaration of the book element, (along with its nested child elements and its attribute) is completed by closing off the complexType and element constructs.
|
</xsd:complexType>
</xsd:element>
|
|
Finally, the schema construct itself is closed:
|
|
|
|
The entire XSD schema definition appears as follows:
|
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="category">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Fiction"/>
<xsd:enumeration value="Non-Fiction"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
|
|
This schema can now be implemented using an XML document, such as our familiar example:
|
<?xml version=”1.0”?>
<book xmlns="http://www.xmltc.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="book.xsd"
category=”Fiction”>
<title>Joy of Integration</title>
<author>Joe Smith</author>
</book>
|
|
The above introduction to XSD schemas has only scratched the surface of this technology’s capabilities. The XML Schema Definition Language provides a number of advanced content modeling features, including:
|
• support for a wide variety of data types
• advanced validation syntax
• the ability to simulate relational schema models through the use of constraints and keys
• extensibility via the any element
• modularization using the include and import elements
• a form of validation inheritance using the redefine element
|