XML Schema (W3C)
XSD, a recommendation of the World Wide Web Consortium, specifies how to formally describe the elements in an Extensible Markup Language document. It can be used by programmers to verify each piece of item content in a document. They can check if it adheres to the description of the element it is placed in.
Like all XML schema languages, XSD can be used to express a set of rules to which an XML document must conform in order to be considered "valid" according to that schema. However, unlike most other schema languages, XSD was also designed with the intent that determination of a document's validity would produce a collection of information adhering to specific data types. Such a post-validation infoset can be useful in the development of XML document processing software.
History
XML Schema, published as a W3C recommendation in May 2001, is one of several XML schema languages. It was the first separate schema language for XML to achieve Recommendation status by the W3C. Because of confusion between XML Schema as a specific W3C specification, and the use of the same term to describe schema languages in general, some parts of the user community referred to this language as WXS, an initialism for W3C XML Schema, while others referred to it as XSD, an initialism for XML Schema Definition. In Version 1.1 the W3C has chosen to adopt XSD as the preferred name, and that is the name used in this article.In its appendix of references, the XSD specification acknowledges the influence of DTDs and other early XML schema efforts such as DDML, SOX, XML-Data, and XDR. It has adopted features from each of these proposals but is also a compromise among them. Of those languages, XDR and SOX continued to be used and supported for a while after XML Schema was published. A number of Microsoft products supported XDR until the release of MSXML 6.0 in December 2006. Commerce One, Inc. supported its SOX schema language until declaring bankruptcy in late 2004.
The most obvious features offered in XSD that are not available in XML's native Document Type Definitions are namespace awareness and datatypes, that is, the ability to define element and attribute content as containing values such as integers and dates rather than arbitrary text.
The XSD 1.0 specification was originally published in 2001, with a second edition following in 2004 to correct large numbers of errors. XSD 1.1 became a W3C Recommendation in .
Schemas and schema documents
Technically, a schema is an abstract collection of metadata, consisting of a set of schema components: chiefly element and attribute declarations and complex and simple type definitions. These components are usually created by processing a collection of schema documents, which contain the source language definitions of these components. In popular usage, however, a schema document is often referred to as a schema.Schema documents are organized by namespace: all the named schema components belong to a target namespace, and the target namespace is a property of the schema document as a whole. A schema document may include other schema documents for the same namespace, and may import schema documents for a different namespace.
When an instance document is validated against a schema, the schema to be used for validation can either be supplied as a parameter to the validation engine, or it can be referenced directly from the instance document using two special attributes,
xsi:schemaLocation
and xsi:noNamespaceSchemaLocation
. XML Schema Documents usually have the filename extension ".xsd". A unique Internet Media Type is not yet registered for XSDs, so "application/xml" or "text/xml" should be used, as per RFC 3023.
Schema components
The main components of a schema are:- Element declarations, which define properties of elements. These include the element name and target namespace. An important property is the type of the element, which constrains what attributes and children the element can have. In XSD 1.1, the type of the element may be conditional on the values of its attributes. An element may belong to a substitution group; if element E is in the substitution group of element H, then wherever the schema permits H to appear, E may appear in its place. Elements may have integrity constraints: uniqueness constraints determining that particular values must be unique within the subtree rooted at an element, and referential constraints determining that values must match the identifier of some other element. Element declarations may be global or local, allowing the same name to be used for unrelated elements in different parts of an instance document.
- Attribute declarations, which define properties of attributes. Again the properties include the attribute name and target namespace. The attribute type constrains the values that the attribute may take. An attribute declaration may also include a default value or a fixed value
- Simple and complex types. These are described in the following section.
- Model group and attribute group definitions. These are essentially macros: named groups of elements and attributes that can be reused in many different type definitions.
- An attribute use represents the relationship of a complex type and an attribute declaration, and indicates whether the attribute is mandatory or optional when it is used in that type.
- An element particle similarly represents the relationship of a complex type and an element declaration, and indicates the minimum and maximum number of times the element may appear in the content. As well as element particles, content models can include model group particles, which act like non-terminals in a grammar: they define the choice and repetition units within the sequence of permitted elements. In addition, wildcard particles are allowed, which permit a set of different elements.
Types
Simple types constrain the textual values that may appear in an element or attribute. This is one of the more significant ways in which XML Schema differs from DTDs. For example, an attribute might be constrained to hold only a valid date or a decimal number.XSD provides a set of 19 primitive data types. It allows new data types to be constructed from these primitives by three mechanisms:
- restriction,
- list, and
- union.
The mechanisms available for restricting data types include the ability to specify minimum and maximum values, regular expressions, constraints on the length of strings, and constraints on the number of digits in decimal values. XSD 1.1 again adds assertions, the ability to specify an arbitrary constraint by means of an XPath 2.0 expression.
Complex types describe the permitted content of an element, including its element and text children and its attributes. A complex type definition consists of a set of attribute uses and a content model. Varieties of content model include:
- element-only content, in which no text may appear
- simple content, in which text is allowed but child elements are not
- empty content, in which neither text nor child elements are allowed
- mixed content, which permits both elements and text to appear
Post-Schema-Validation Infoset
After XML Schema-based validation, it is possible to express an XML document's structure and content in terms of the data model that was implicit during validation. The XML Schema data model includes:- The vocabulary
- The content model
- The data types
Secondary uses for XML Schemas
The primary reason for defining an XML schema is to formally describe an XML document; however the resulting schema has a number of other uses that go beyond simple validation.Code generation
The schema can be used to generate code, referred to as XML Data Binding. This code allows contents of XML documents to be treated as objects within the programming environment.Generation of XML file structure documentation
The schema can be used to generate human-readable documentation of an XML file structure; this is especially useful where the authors have made use of the annotation elements. No formal standard exists for documentation generation, but a number of tools are available, such as the Xs3p stylesheet, that will produce high quality readable HTML and printed material.Criticism
Although XML Schema is successful in that it has been widely adopted and largely achieves what it set out to, it has been the subject of a great deal of severe criticism, perhaps more so than any other W3C Recommendation.Good summaries of the criticisms are provided by James Clark, Anders Møller and Michael Schwartzbach, Rick Jelliffe and David Webber.
General problems:
- It is too complicated, so it is hard to use by non-experts - but many non-experts need schemas to describe data formats. The W3C Recommendation itself is extremely difficult to read. Most users find much easier to understand.
- XSD lacks any formal mathematical specification.
- There are many surprises in the language, for example that restriction of elements works differently from restriction of attributes.
- XSD offers very weak support for unordered content.
- XSD cannot require a specific root element.
- When describing mixed content, the character data cannot be constrained in any way.
- Content and attribute declarations cannot depend on attributes or element context.
- It is not 100% self-describing, even though that was an initial design requirement.
- Defaults cannot be specified separately from the declarations ; element defaults can only be character data.
- Although it technically is namespace-conformant, it does not seem to follow the namespace spirit.
- XSD 1.0 provided no facilities to state that the value or presence of one attribute is dependent on the values or presence of other attributes. This has been fixed in XSD 1.1.
- The set of XSD datatypes on offer is highly arbitrary.
- The two tasks of validation and augmentation should be kept separate.
Version 1.1
Significant new features in XSD 1.1 are:
- The ability to define assertions against the document content by means of XPath 2.0 expressions.
- The ability to select the type against which an element will be validated based on the values of the element's attributes.
- Relaxing the rules whereby explicit elements in a content model must not match wildcards also allowed by the model.
- The ability to specify wildcards that apply to all types in the schema, so that they all implement the same extensibility policy.