History
XML Schema, published as aSchemas 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 (a process known as ''assessment''), 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
. (The latter mechanism requires the client invoking validation to trust the document sufficiently to know that it is being validated against the correct schema. "xsi" is the conventional prefix for the namespace "http://www.w3.org/2001/XMLSchema-instance".)
XML Schema Documents usually have the filename extension ".xsd". A unique 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 (which is then the only value the attribute may take.) * 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 (perhaps any element provided it is in a certain namespace). Other more specialized components include annotations, assertions, notations, and the schema component which contains information about the schema as a whole.Types
Simple types (also called data 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 (anyURI
, base64Binary
, boolean
, date
, dateTime
, decimal
, double
, duration
, float
, hexBinary
, gDay
, gMonth
, gMonthDay
, gYear
, gYearMonth
, NOTATION
, QName
, string
, and time
). It allows new data types to be constructed from these primitives by three mechanisms:
* restriction (reducing the set of permitted values),
* list (allowing a sequence of values), and
* union (allowing a choice of values from several types).
Twenty-five derived types are defined within the specification itself, and further derived types can be defined by users in their own schemas.
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 (other than whitespace, or text enclosed by a child element)
* 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
A complex type can be derived from another complex type by restriction (disallowing some elements, attributes, or values that the base type permits) or by extension (allowing additional attributes and elements to appear). In XSD 1.1, a complex type may be constrained by assertions— XPath 2.0 expressions evaluated against the content that must evaluate to true.
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 (element and attribute names) * The content model (relationships and structure) * The data types This collection of information is called the Post-Schema-Validation Infoset (PSVI). The PSVI gives a valid XML document its "type" and facilitates treating the document as an object, usingSecondary 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 (the spec is several hundred pages in a very technical language), 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 finVersion 1.1
XSD 1.1 became aSee also
* List of types of XML schemas – list of XML schemas in use on the Internet sorted by purpose * RELAX NG – another XML schema language (an ISO international standard) that is often used with XSD datatypes * XML Schema editors – Information about XSD Tools * XML schema languages – Compares XSD to other XML schema languages * Unique Particle Attribution * Canonical modelReferences
Further reading
*''Definitive XML Schema'', Priscilla Walmsley, Prentice-Hall, 2001, *''XML Schema'', Eric van der Vlist, O'Reilly, 2001, *''The XML Schema Companion'', Neil Bradley, Addison-Wesley, 2003, *''Professional XML Schemas'', Jon Ducket et al., Wrox Press, 2001, *''XML Schemas'', Lucinda Dykes et al., Sybex,External links
W3C XML Schema 1.0 Specification