NEW TITLE RELEASE ANNOUNCEMENT
See the latest about a temporary release delay on new titles. Learn more.
The authoritative XML Schema reference and tutorial!
To leverage the full power of XML, companies need shared vocabularies to base their documents and scripts upon. XML Schema makes it possible to create those shared vocabularies-and Definitive XML Schema is the authoritative guide to the standard! Written by Priscilla Walmsley, a member of the W3C working group that created XML Schema, this book explains the W3C Recommendation with unprecedented insight and clarity–and introduces practical techniques for writing schemas to support any B2B, Web service, or content processing application. Coverage includes:
Definitive XML Schema brings together expert guidance for schema design, superior approaches to schema development, and the most systematic XML Schema reference on the market. Whether you're a developer, architect, or content specialist, it's the only XML Schema resource you need!
"XML Schema is an incredibly powerful-and complex-document schema language, with such new capabilities as strong typing, modularity, inheritance, and identity constraints. This book guides you through the complexity so you can confidently use that power for your own projects."
–Charles F. Goldfarb
Visit the Author's Web Site related to this title.
1. Schemas: An introduction.
What is an XML schema? The purpose of schemas. Schema design. Schema languages.
An example schema. The components of XML Schema. Elements and attributes. Data types. Simple types. Complex types. Namespaces and XML Schema. Schema composition. Instances and schemas. Annotations. Advanced features.
Namespaces in XML. The relationship between namespaces and schemas. Using namespaces in XSDL.
Modularizing schema documents. Defining schema documents. Schema assembly. include, redefine, and import.
Using the instance attributes. Schema processing. Relating instances to schemas. Using XSDL hints in the instance. Dereferencing namespaces. The root element. Using DTDs and schemas together. Using specific schema processors.
The mechanics. User documentation. Application information. Notations.
Global and local element declarations. Declaring the data types of elements. Default and fixed values. Nils and nillability. Qualified vs unqualified forms.
Global and local attribute declarations. Assigning types to attributes. Default and fixed values. Qualified vs unqualified forms.
Simple type varieties. Simple type definitions. Simple type restrictions. Facets. Preventing simple type derivation.
The structure of a regular expression. Atoms. Quantifiers.
Varieties and derivation types. Union types. List types.
Built-in types. String-based types. Numeric types. Date and time types. Legacy types. Other types. Type equality.
What are complex types? Defining complex types. Content types. Using element types. Using model groups. Using attributes.
Why derive types? Restriction and extension. Simple content and complex content. Complex type extensions. Complex type restrictions. Type substitution. Controlling type derivation and substitution.
Why reusable groups? Named model groups. Attribute groups. Reusable groups vs complex type derivations.
Why substitution groups? The substitution group hierarchy. Declaring a substitution group. Type constraints for substitution groups. Alternatives to substitution groups. Controlling substitution groups.
Identity constraint categories. Design hint: Should I use ID/IDREF or key/keyref? Structure of an identity constraint. Uniqueness constraints. Key constraints. Key references. Selectors and fields. The XML Schema XPath subset. Identity constraints and namespaces.
Redefinition basics. The mechanics of redefinition. Redefining simple types. Redefining complex types. Redefining named model groups. Redefining attribute groups.
Element declarations. Attribute declarations. Notations. Parameter entities for reuse. Parameter entities for extensibility. External parameter entities. General entities. Comments. Using DTDs and schemas together.
Naming guidelines. Qualified vs unqualified names. Structuring namespaces. Multiple languages.
Reuse. Extending schemas. Versioning of schemas. Designing applications to support change.
XSDL element types. XSDL attributes.
Built-in simple types.
This chapter provides a brief explanation of schemas and why they are important. It also discusses the basic schema design goals, and describes the various existing schema languages.
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 in Example 1-1. It consists of a product element that has two children (number and size) and an attribute (effDate).
Example 1-2 shows a schema that describes the instance. It contains element and attribute declarations that assign data types and element-type names to elements and attributes.
<product effDate="2001-04-02"> <number>557</number> <size>10</size></product>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="product" type="ProductType"/> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element name="number" type="xsd:integer"/> <xsd:element name="size" type="SizeType"/> </xsd:sequence> <xsd:attribute name="effDate" type="xsd:date"/> </xsd:complexType> <xsd:simpleType name="SizeType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="2"/> <xsd:maxInclusive value="18"/> </xsd:restriction> </xsd:simpleType></xsd:schema>
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:
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.
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.
Schema processing can also add to the instance. It inserts default and fixed values for elements and attributes, and normalizes whitespace according to the data type.
Schemas provide a way for additional information about the data to be supplied to the application when processing a particular type of document. For example, you could include information on how to map the product element instances to a database table, and have the application use this information to automatically update a particular table with the data.
In addition to being available at processing time, this information in schemas can be used to generate code such as:
Tools have only just begun to take advantage of the possibilities of schemas. In the coming years, we will see schemas used in many creative new ways.
XML Schema is packed with features, and there are often several ways to accurately describe the same thing. The decisions made during schema design can affect its usability, accuracy, and applicability. Therefore, it is important to keep in mind your design objectives when creating a schema. These objectives may vary depending on how you are using XML, but some are common to all use cases.
Obviously, a schema should accurately describe an XML instance and allow it to be validated. Schemas should also be precise in describing data. Precision can result in more complete validation as well as better documentation. Precision can be achieved by defining restrictive data types that truly represent valid values.
Schemas should be very clear, allowing a reader to instantly understand the structure and characteristics of the instance being described. Clarity can be achieved by:
There is a temptation to create schemas that are useful only for a specific application purpose. In some cases, this may be appropriate. However, it is better to create a schema that has broader applicability. For example, a business unit that handles only domestic accounts may not use a country element declaration as part of an address. They should consider adding it in as an optional element for the purposes of consis-tency and future usability.
There are two components to a schema's broad applicability: reusability and extensibility. Reusable schema components are modular and well documented, encouraging schema authors to reuse them in other schemas. Extensible components are flexible and open, allowing other schema authors to build on them for future uses. Since reusability and extensibility are important, all of Chapter 21, "Extensibility and reuse," is devoted to them.
Document Type Definitions (DTDs) are a commonly used method of describing XML documents. They allow you to define the basic structure of an XML instance, including:
Example 1-3 shows a DTD that is roughly equivalent to our schema in Example 1-2.
<!ELEMENT product (name, size?)><!ELEMENT name (#PCDATA)><!ELEMENT size (#PCDATA)><!ATTLIST product effDate CDATA #IMPLIED>
DTDs have many advantages. They are relatively simple, have a compact syntax, and are widely understood by XML implementers. When designed well, they can be extremely modular, flexible, and extensible.
However, DTDs also have some shortcomings. They have their own non-XML syntax, do not support namespaces easily, and provide very limited data typing, for attributes only.
As XML became increasingly popular for data applications such as e-commerce and enterprise application integration (EAI), a more robust schema language was needed. Specifically, XML developers wanted:
DTDs are not likely to disappear now that schemas have arrived on the scene. They are supported in many tools, are widely understood, and are currently in use in many applications. In addition, they continue to be useful as a lightweight alternative to schemas.
Four schema languages were developed before work began on XML Schema: XDR (XML Data Reduced), DCD, SOX, and DDML. These four languages were considered together as a starting point for XML Schema, and many of their originators were involved in the creation of XML Schema.
The World Wide Web Consortium (W3C) began work on XML Schema in 1998. The first version, upon which this book is based, became an official Recommendation on May 2, 2001. The formal Recommendation is in three parts:
"XML Schema" is the official name of the Recommendation and is also sometimes used to refer to conforming schema documents. In order to clearly distinguish between the two, this book uses the term "XML Schema" only to mean the Recommendation itself.
A "schema definition" is the formal expression of a schema.
The initialism "XSDL" (XML Schema Definition Language) is used to refer to the language that is used to create schema definitions in XML. In other words, XSDL is the markup language that uses elements such as schema and complexType.
The term "schema document" is used to refer to an XML document that is written in XSDL, with a schema element as its root. The extension "xsd" is used in the file identifiers of such documents. A schema definition may consist of one or more schema documents, as described in Chapter 4, "Schema composition."
As it is unlikely to cause confusion in this book, for simplicity the word "schema" will be used to refer to both a schema as a concept, and an actual schema definition that conforms to the XML Schema definition language.
According to the XML Recommendation, every XML element has an element type. In fact, it is the name of the element type that occurs in the start- and end-tags, as individual elements do not have names (although they may have IDs).
XML Schema, however, uses the word "type" exclusively as a shorthand to refer to simple types and complex types. Perhaps to avoid confusion with this usage, the Recommendation does not use the phrase "element type" in conjunction with schemas. This book follows that same practice and generally doesn't speak of element types per se, although it does refer to "element-type names" where appropriate.
XML Schema is not the only schema language that is currently in use. While it is very robust, it is not always the most appropriate schema language for all cases. This section describes two other schema languages.
RELAX NG covers some of the same ground as XML Schema. As of this writing, it is currently being developed by an OASIS technical committee. RELAX NG is intended only for validation; the processor does not pass documentation or application information from the schema to the application. RELAX NG does not have built-in data types; it is designed to use other data type libraries (such as that of XML Schema).
RELAX NG has some handy features that are not currently part of XML Schema:
However, RELAX NG also has some limitations compared to XML Schema:
For more information on RELAX NG, see
Schematron takes a different approach from XML Schema and RELAX NG. XML Schema and RELAX NG are both grammar-based schema languages. They specify what must appear in an instance, and in what order.
By contrast, Schematron is rule-based. It allows you to define a series of rules to which the document must conform. These rules are expressed using XPath. In contrast to grammar-based languages, Schematron considers anything that does not violate a rule to be valid. There is no need to declare every element type or attribute that may appear in the instance.
Like RELAX NG, Schematron is intended only for validation of instances. It has a number of advantages:
The limitations of Schematron compared to XML Schema are:
Because Schematron and XML Schema complement each other, it makes sense to combine the two. An example of embedding a Schematron schema in XSDL is provided in Section 6.3.2, "Schematron for co-occurrence constraints." For more information on Schematron, see