Introduction to XML Schemas
What is a schema?
The word schema means a diagram, plan, or framework. In XML, it refers to a document
that describes an XML document. Suppose you have the XML instance shown below.
<product effdate="2001-04-12">
<number>557</number>
<size>10</size>
</product>
The instance consists of a product element that has two children (number and
size) and an attribute (effDate).
The sentence you just read could be a schema because it describes the instance document, and
all other instances of a product with the same kind of children and attribute. However,
to be useful with XML and benefit from computer processing, this kind of schema won't do.
The schema must be defined in a schema document using a formal schema language.
Shown below is a schema document that describes the instance. It contains element and attribute
declarations that assign types and names to elements and attributes. The document is written in
the XML Schema Definition Language (XSD).
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="product" type="ProducType"/>
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="number" type="xs:integer"/>
<xs:element name="size" type="SizeType"/>
</xs:sequence>
<xs:attribute name="effdate" type="xs:date"/>
</xs:complexType>
<xs:simpleType name="SizeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="2"/>
<xs:maxInclusive value="18"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
In contrast, an equally valid schema isn the familiar Document Type Definition (DTD) language of XML.
The disproportionate sizes of the two schema documents roughly reflect the capabilities of the two
languages. The extensive features of XML Schema, when to use them, and how to use them effectively,
form the subject matter of this book.
The purpose of schemas
Schemas are developed for many purposes. How effectively a schema can acheive them depends on
the capabilities of the schema language and the schema processor, as well as the quality of the schema design.
Data validation
One of the most common uses for schemas is to verify that an XML document is valid according to a
defined set of rules. A schema can be used to validate:
• The structure of elements and attributes. For example, a product must have a
numner and a size, and may optionally have an effDate.
• The order of elements. For example, number must appear before size.
• The data values of elements and attributes, based on ranges, enumerations, and pattern matching.
For example, size must be an integer between 2 and 18, and effdate must be a valid date.
• The uniqueness of values in an instance. For example, all product numbers in an instance must be unique.
A contract with trading partners
Often, XML instances are passed between organizations. A schema may act as a contract with your
trading partners. It clearly lays out the rules for document structure and what is required.
Since an instance can be validated against a schema, the "contract" can be enforced using available tools.
System documentation
Schemas can provide documentation about the data in an XML instance. Anyone who needs to understand
the data can refer to the schema for information about names, structures, and data types of the items.
To include further documentation, you can add annotations to any schema component.
|