XML Schemas

On May 2, 2001, the standards body governing over the XML standard announced that an important member of the XML family has reached standard status (a proposed recommendation, as www.w3.org calls it). This standard is called XML Schemas and is poised to replace DTDs as the preferred way to validate XML documents. XML Schemas offer two distinct advantages over DTDs:

• An XML Schema is an XML document

• XML Schemas allow you to specify data characteristics (such as type, size and precision) of elements and attributes

A schema documents looks as follows:

<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2 0 01/XMLSchema">

The xmlns:xsd attribute of the schema element is a namespace declaration, which is covered in the next section. Note that the value of this attribute has changed over time, so if you encounter a schema with a different value for this attribute (for example,

www.w3.org/2000/10/XMLSchema), then this schema was properly created according to a draft version of the XML Schema standard.

The schema content consist of definitions for the elements and attributes that this schema can contain. An element is defined as follows

<xsd:element name="theElementName"> <! -- element specifics goes here --> </xsd:element>

while an attribute is defined as follows

<xsd:attribute name="theAttributeName"> <! -- attribute specifics goes here --> </xsd: attribute >

You can add documentation with comments or stick an annotation element inside the element or attribute definition. The annotation element contains a documentation element where you can document the specifics of the element or attribute.


<xsd:documentation>Some explanation here...</xsd:documentation> </xsd:annotation>

You can group elements and attributes by sticking them inside a complexType tag.

<xsd:complexType> </xsd:complexType>

This type of grouping is required each time you see an element definition such as this:

<!ELEMENT Book (Title,Category*,Author+)>

Elements grouped within a sequence need to be presented in the order they are defined.

<xsd:sequence> </xsd:sequence>

So, if you define a Book element as follows, then a Book element needs to contain a Title, Category and Author element in this exact order (Title, Author and Category would for example not be valid).

<xsd:element name="Book"> xsd:complexType> <xsd:sequence> <xsd:element name="Title"> </xsd:element>

<xsd:element name="Category"/> <xsd:element name="Author"/> </xsd:sequence> </xsd:complexType> </xsd:element>

The cardinality of elements is assumed one. If you want to create a repeating element, you may do so by adding a maxOccurs="unbounded" attribute to the element definition. If you want to create an optional element, you may do so by adding a min0ccurs="0" attribute to the element definition. You may of course combine these attributes to create an optional repeating element.

Finally, you can specify the data type of an element with a type="xsd:datatype" attribute. In our example, we use only the string data type. The XML Schema allows for a wide range of data types, such as integer, long, date, time, double, float, and so on.

Listing A-5 lists the XML Schema that corresponds to the DTD discussed above. XML

Schemas have a .xsd file extension and are therefore sometimes called XSDs.

Listing A-5: XML Schema Corresponding to DTD_

<?xml version="1.0" encoding="UTF-8"?> <!-- W3C Schema for a List of Books -->

<xsd:schema xmlns:xsd="http://www.w3.org/2 0 01/XMLSchema"> <xsd:element name="XMLBooks"> <xsd:annotation>

<xsd:documentation>The top-level element, XMLBooks, is a list of books.</xsd:documentation> </xsd:annotation> <xsd:complexType>


<xsd:element name="Book"



<xsd:documentation>A Book element contains 1 Title, 1 or more Category, and 1 or more Author.</xsd:documentation>

</xsd:annotation> <xsd:complexType>


<xsd:element name="Title" type="xsd:string"> <xsd:annotation>

<xsd:documentation>The Title, Category, and Author elements contain text.</xsd:documentation>


</xsd:element> <xsd:element name="Category" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>

<xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/>

</xsd:sequence> <xsd:attribute name="ISBN" type="xsd:string" use="required" id="isbn">


<xsd:documentation>A Book has 1 required attribute.</xsd:documentation>

</xsd:annotation> </xsd:attribute> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>

Listing A-6 shows how an XML document can refer to its associated XML Schema. Listing A-6: XML Document That Refers to Its Associated XML Schema_

<?xml version="1.0" encoding="UTF-8"?>

<XMLBooks xmlns:xsi="http://www.w3.org/2 0 01/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./Books.xsd"> <Book ISBN="0-7 8 97-22 42-9">

<Title>XML By Example</Title> <Category>Web Development</Category> <Author>Benoit Marchal</Author>

<Title>XML in Action</Title> <Category>Internet</Category> <Category>XML</Category> <Author>William J. Pardy</Author> </Book> </XMLBooks>

0 0

Post a comment