Book HomeXML in a NutshellSearch this book

21.2. Schema Elements

The W3C XML Schema Language defines 42 elements, which naturally divide into several categories:

One root element
xs:schema

Three declaration elements
xs:element, xs:attribute, and xs:notation

Eight elements for defining types
xs:complexContent, xs:complexType, xs:extension, xs:list, xs:restriction, xs:simpleContent, xs:simpleType, and xs:union

Seven elements for defining content models
xs:all, xs:any, xs:anyAttribute, xs:attributeGroup, xs:choice, xs:group, and xs:sequence

Five elements for specifying identity constraints
xs:field, xs:key, xs:keyref, xs:selector, and xs:unique

Three elements for assembling schemas out of component parts
xs:import, xs:include, and xs:redefine

12 facet elements for constraining simple types
xs:enumeration, xs:fractionDigits, xs:length, xs:maxExclusive, xs:maxInclusive, xs:maxLength, xs:minExclusive, xs:minInclusive, xs:minLength, xs:pattern, xs:totalDigits, and xs:whiteSpace

Three elements for documenting schemas
xs:appinfo, xs:annotation, and xs:documentation

Elements in this section are arranged alphabetically from xs:any to xs:whiteSpace. Each element begins with a sample implementation in the following form:

<xs:elementName
   attribute1 = "allowed attribute values"
   attribute2 = "allowed attribute values"
>
  <!-- Content model -->
</xs:elementName>

Most attribute values can be expressed as one of the 44 XML Schema built-in simple types, such as xs:string, xs:ID, or xs:integer. Some attribute values are specified as an enumeration of the legal values in the form ( value1 | value2 | value3 | etc. ). In this case, the default value, if there is one, is given in boldface.

Element content models are given in a comment in the form they might appear in an ELEMENT declaration in a DTD. For example, an xs:all element may contain a single optional xs:annotation child element followed by zero or more xs:element elements. Thus its content model is written like this:

<!-- ( xs:annotation?, xs:element* ) -->
xs:all

<xs:all
  id = "ID"
  maxOccurs = "1"
  minOccurs = "(0 | 1)">
  <!-- ( xs:annotation?, xs:element* ) -->
</xs:all>

The xs:all element indicates that every element represented by one of its child xs:element elements must appear. However, the order of the child elements in the instance element does not matter. For example, an xs:all element can require that each FullName element have exactly one FirstName child and exactly one LastName child, but that the order of the two child elements does not matter; the first name can come first or the last name can come first.

The xs:all element must be the top group in its content model (i.e., an xs:group, xs:choice, or xs:sequence cannot contain an xs:all element). The complete group represented by the xs:all element can occur either zero or one time as indicated by its minOccurs and maxOccurs attributes. By default it must occur exactly once. Furthermore, the minOccurs attribute of each of the individual xs:element elements inside the xs:all element must also be set to either 0 or 1, and the maxOccurs attribute of each of these elements must be set to 1. xs:all cannot indicate, for example, that a FullName element must contain between zero and five FirstName s and between one and three LastName s in any order.

xs:annotation

<xs:annotation 
  id = "ID">
  <!-- ( xs:appinfo | xs:documentation )* -->
</xs:annotation>

The xs:annotation element is ignored by schema validators. Its purpose is to provide metainformation about the schema or schema element in which it appears. Information intended for human readers is placed in xs:documentation child elements. Information intended for software programs is placed in xs:appinfo child elements.

xs:any

<xs:any
  id = "ID"
  maxOccurs = "nonNegativeInteger | unbounded"
  minOccurs = "nonNegativeInteger"
  namespace = " ##any | ##other | anyURI* ##targetNamespace? ##local? "
  processContents = " lax | skip | strict ">
  <!-- xs:annotation? -->
</xs:any>

The wildcard element xs:any is useful when writing schemas for languages such as XSLT that routinely include markup from multiple vocabularies that are unknown when the schema is written. It indicates that between minOccurs and maxOccurs, elements from one or more namespaces identified by the namespace attribute may appear at that position in a content model. As well as literal namespace URIs, the special value ##targetNamespace can be included in the list to indicate that any element from the schema's target namespace can be used. The special value ##local can be included in the list to indicate that elements not in any namespace can be used. Instead of the list of namespace URIs, you can use the special value ##any to indicate that all elements from any namespace or no namespace are allowed, or the special value ##other to indicate that elements from namespaces other than the schema's target namespace can be used.

The processContents attribute indicates whether the elements represented by xs:any have to be declared or whether they can be completely unfamiliar to the schema. It has one of these three values:

strict
Elements represented by this xs:any element must be declared or have an xsi:type attribute. Furthermore, the element must be valid according to its declaration or type.

skip
Elements represented by this xs:any element need not be declared in the schema and need not be valid even if they are declared.

lax
Elements represented by this xs:any element must be validated if they are declared or if they have an xsi:type attribute, but must not be validated if they are neither declared nor have an xsi:type attribute.

The default value is strict.

xs:anyAttribute

<xs:anyAttribute
  id = "ID"
  namespace = "##any | ##other | anyURI* ##targetNamespace? ##local?"
  processContents = "(lax | skip | strict)" >
  <!-- (xs:annotation?) -->
</xs:anyAttribute>

The xs:anyAttribute element appears inside xs:complexType elements, where it indicates that elements of that type can have any attribute from one or more namespaces. It can also appear inside xs:attributeGroup elements, where it adds attributes from one or more namespaces as potential members of the group. The namespace attribute contains a whitespace-separated list of the namespace URIs that are allowed for this element's attributes. As well as literal namespace URIs, the special value ##targetNamespace can be included in the list to indicate that any attribute from the schema's target namespace can be used. The special value ##local can be included in the list, indicating that attributes not in any namespace (unprefixed attributes) may be used. Instead of the list of namespace URIs, you can use the special value ##any to indicate that all attributes from any namespace are allowed or the special value ##other to indicate that attributes from namespaces other than the schema's target namespace can be used.

The processContents attribute indicates whether the attributes themselves have to be declared, generally as top-level attributes. It has one of these three values:

strict
Attributes represented by this xs:anyAttribute element must be declared, and the attribute must be valid according to its declaration. This is the default.

lax
Attributes represented by this xs:anyAttribute element must be validated if they are declared, but must not be validated if they are not declared.

skip
Attributes represented by this xs:anyAttribute element need not be declared in the schema and need not be valid even if they are declared.

xs:appinfo

<xs:appinfo
  source = "anyURI">
  <!-- any well-formed XML markup -->
</xs:appinfo>

The xs:appinfo element appears exclusively inside xs:annotation elements, where it provides machine-readable information about the schema or schema element it's documenting. It has no effect on schema validation. It can contain absolutely any XML markup: an XSLT stylesheet for the schema, a schema for the schema, a schema in a different schema language such as Schematron, or anything else you can imagine. The only restriction is that the contents must be well-formed. Alternately, instead of or in addition to including this information directly, the source attribute can point to it using a URI.

xs:attribute

<xs:attribute
  default = "string"
  fixed = "string"
  form = "( qualified | unqualified )
  id = "ID"
  name = "NCName"
  ref = "QName"
  type = "QName"
  use = "( optional | prohibited | required )">
  <!-- ( xs:annotation?, xs:simpleType? ) -->
</xs:attribute>

The xs:attribute element declares an attribute. Inside an xs:complexType element it indicates that elements of that type can have an attribute with the specified name and type.

Attributes

id, optional
An XML name unique among all of the ID-type attributes in this schema document.

default, optional
The default value of the attribute reported for those elements in the instance document that do not contain an explicit specification of this attribute.

fixed, optional
A default value for this attribute that may not be overridden in the instance document. An xs:attribute element cannot have both fixed and default attributes.

form, optional
If this has the value qualified, then the attribute must be in the schema's target namespace. If this has the value unqualified, then the attribute must not be in any namespace. The default value for this is set by the attributeFormDefault attribute on the root xs:schema element.

name, optional
The local name of the attribute.

ref, optional
The qualified name of the attribute declared by a top-level xs:attribute element elsewhere in the schema. Either the name or ref attribute should be provided, but not both.

type, optional
The qualified name of the type of the attribute, either a built-in simple type such as xs:integer or a user-defined simple type.

use, optional
One of the three keywords, optional, prohibited, or required, which have the following meanings:

optional
Authors of instance documents may or may not include this attribute as they choose. This is the default.

prohibited
Authors of instance documents must not include this attribute.

required
Authors of instance documents must include this attribute on all elements of the requisite type.

Contents

The xs:attribute element may contain a single xs:annotation element to describe itself. This has no effect on the attribute type.

In place of a type attribute, the xs:attribute element may contain a single xs:simpleType element that provides an anonymous type for the attribute derived from a base simple type.

xs:attributeGroup

<xs:attributeGroup
  id = "ID"
  name = "NCName"
  ref = "QName">
  <!--
    ( xs:annotation?, (xs:attribute | xs:attributeGroup)*,
      xs:anyAttribute? )
   -->
</xs:attributeGroup>

The xs:attributeGroup element is used in two ways. At the top level of the schema, it has a name attribute and defines a new attribute group. The attributes in the group are indicated by the child elements of the xs:attributeGroup element. Inside an xs:complexType element or another xs:attributeGroup, it has a ref attribute but no name and adds the attributes in the referenced group to the type or group's list of attributes.

xs:choice

<xs:choice
  id = "ID"
  maxOccurs = "( nonNegativeInteger | unbounded )"
  minOccurs = "nonNegativeInteger">
  <!--
    ( xs:annotation?, (xs:element | xs:group | xs:choice
    | xs:sequence | xs:any)*)
  -->
</xs:choice>

The xs:choice element indicates that any element or group represented by one of its child elements may appear at that position in the instance document. At least minOccurs elements from the choice must appear. At most maxOccurs elements from the choice must appear. The default for both minOccurs and maxOccurs is 1.

xs:complexContent

<xs:complexContent
  id = "ID"
  mixed = "( true | false )">
  <!-- ( xs:annotation?, (xs:restriction | xs:extension) ) -->
</xs:complexContent>

The xs:complexContent element is used inside xs:complexType elements to derive a new complex type from an existing complex type by restriction or extension. When deriving by extension, the mixed attribute must have the same value as the base type's mixed attribute. When deriving by restriction, the mixed attribute can have the value false to disallow mixed content that would be allowed in the base type. It can have the value true only if the base type allows mixed content. In other words, a derived type can disallow mixed content that's allowed in the base type, but cannot allow it if the base type doesn't already allow it.

xs:complexType

<xs:complexType
  abstract = "( true | false )"
  block = "( #all | extension | restriction )"
  final = "( #all | extension | restriction )"
  id = "ID"
  mixed = "( true | false )"
  name = "NCName"
  >
  <!-- ( xs:annotation?, (xs:simpleContent | xs:complexContent
       | ((xs:group | xs:all | xs:choice | xs:sequence)?,
         ((xs:attribute | xs:attributeGroup)*, xs:anyAttribute?)))) -->
</xs:complexType>

The xs:complexType element defines a new complex type, that is, an element type that can potentially contain either child elements, attributes, or both. The valid child elements and attributes for elements of this type are specified by the contents of the xs:complexType element. The mixed attribute specifies whether the complex type is allowed to contain text interspersed with its child elements. If the xs:complexType element is a top-level element, then it has a name attribute and defines a named type. Otherwise, if the xs:complexType element appears inside an xs:element element, then it does not have a name attribute and defines an anonymous type for that element alone.

If the abstract attribute has the value true, then no elements of this type can be included in instance documents--only elements of subtypes derived from this type, which are marked as elements of this type by an xsi:type attribute. If the final attribute has the value restriction, then this type cannot be subtyped by restriction. If the final attribute has the value extension, then this type cannot be subtyped by extension. If the final attribute has the value #all, then this type cannot be subtyped by either restriction or extension. The default value of the final attribute is set by the finalDefault attribute on the root xs:schema element. If the block attribute has the value extension or restriction, then instances of this type cannot be replaced in instance documents by instances of subtypes derived from this type by extension or restriction, respectively, though such subtypes may still be defined and used for other elements. If the block attribute has the value #all, then this type cannot be replaced in instance documents by instances of any subtype. The default value of the block attribute is set by the blockDefault attribute on the root xs:schema element.

xs:documentation

<xs:documentation
  source = "anyURI"
  xml:lang = "language">
  <!-- any well-formed XML markup -->
</xs:documentation>

The xs:documentation element appears exclusively inside xs:annotation elements where it provides human-readable information about the schema or schema element it's annotating. It has no effect on schema validation. It can contain absolutely any XML markup: XHTML, DocBook, or just plain text. The only restriction is that the contents must be well-formed. Alternately, instead of or in addition to including this information directly, the source attribute can point to it using a URI. The xml:lang attribute can be used to indicate the language in which the description is written. You could even include multiple xs:documentation elements in different languages.

xs:element

<xs:element
  abstract = "( true | false )"
  block = "( #all | extension | restriction | substitution )"
  default = "string"
  final = "( #all | extension | restriction )"
  fixed = "string"
  form = "( qualified | unqualified )"
  id = "ID"
  maxOccurs = "( nonNegativeInteger | unbounded )"
  minOccurs = "nonNegativeInteger"
  name = "NCName"
  nillable = "( true | false )"
  ref = "QName"
  substitutionGroup = "QName"
  type = "QName">
  <!-- ( xs:annotation?,
       ((xs:simpleType | xs:complexType)?,
       (xs:unique | xs:key | xs:keyref)*) ) -->
</xs:element>

The xs:element element declares an element, including its name and type. Used at the top level of the schema, it indicates a potential root element. Used inside an xs:complexType element, it indicates a potential child element of another element. Alternately, instead of specifying a name and a type, it can have a ref attribute that points to an element declaration elsewhere in the schema.

Attributes

id, optional
id is an XML name unique within ID-type attributes in this schema document.

abstract, optional
If the abstract attribute has the value true, then only elements from this element's substitution group are allowed in instance documents, not elements of this type itself.

default, optional
default is the default value of the element reported for empty elements of this type in the instance document.

block, optional
If the block attribute contains the value extension, restriction, or substitution, then this element cannot be replaced in instance documents and substitution groups by instances of subtypes derived from this element's type by extension, restriction, or substitution, respectively. If the block attribute has the value #all, then this element cannot be replaced in instance documents by instances of any subtype of the element's type.

final, optional
The final attribute controls which elements can refer to this element as the head of their substitution group. If the value contains the keyword restriction, then restrictions of this element's type cannot do so. If the value contains the keyword extension, then extensions of this element's type cannot do so. If the value is #all, then neither extensions nor restrictions of this type can do so.

form, optional
If the form attribute has the value qualified, then the element is in the schema's target namespace. If it has the value unqualified, then the element is not in any namespace. The default value is set by the elementFormDefault attribute on the root xs:schema element. This attribute can only be used on locally declared elements. All globally declared elements are always in the schema's target namespace.

maxOccurs, optional
This signifies the maximum number of times this element may be repeated in valid instance documents.

minOccurs, optional
This signifies the minimum number of times this element must be repeated in valid instance documents.

name, optional
This represents the name of the element.

nillable, optional
If nillable has the value true, then this element can be specified as being "nil" using an xsi:nil="true" attribute in the instance document.

ref, optional
This represents the qualified name of the element declared by a top-level xs:element element elsewhere in the schema.

type, optional
This is the qualified name of the type of the element, either a built-in simple type such as xs:integer or a user-defined type.

substitutionGroup, optional
This is the qualified name of a globally declared element for which this element may substitute in instance documents.

Contents

The xs:element element may contain an optional xs:annotation. If and only if the xs:element element does not have a type attribute, then it must have either an xs:simpleType child element or an xs:complexType child element that provides an anonymous type for this element. Finally, it may have any number of xs:key, xs:keyref, and xs:unique elements to set uniqueness and identity constraints.

xs:enumeration

<xs:enumeration
  id = "ID"
  value = "anySimpleType">
  <!-- (xs:annotation?) -->
</xs:enumeration>

The xs:enumeration facet element is used inside xs:restriction elements to derive new simple types by listing all valid values. The value attribute contains a single valid value of the type specified by the parent xs:restriction's base attribute. This xs:restriction element contains one xs:enumeration child element for each valid value.

xs:extension

<xs:extension
  base = "QName"
  id = "ID">
  <!-- (xs:annotation?,
       ((xs:group | xs:all | xs:choice | xs:sequence)?,
       ((xs:attribute | xs:attributeGroup)*, xs:anyAttribute?))) -->
</xs:extension>

The xs:extension element is used inside xs:simpleContent and xs:complexContent elements to derive a new complex type that adds attributes and/or child elements not present in the base type. The base type being extended is given by the value of the base attribute. The child elements and attributes added to the base type's content model are specified by the content of the xs:extension element. An instance of such an extended type must have all the child elements required by the base type followed by all the child elements required in the xs:extension.

xs:field

<xs:field
  id = "ID"
  xpath = "XPath expression">
  <!-- (xs:annotation?) -->
</xs:field>

One or more xs:field elements are placed inside each xs:unique, xs:key, and xs:keyref element to define a value calculated by the XPath expression in the xpath attribute. The context node for this expression is set in turn to each element in the node set selected by the xs:selector element.

Not all XPath expressions are allowed here. In particular, the XPath expression must limit itself to the child axis, except for the last step, which may use the attribute axis. The only node tests used are name tests (element and attribute names, the * wildcard, and the prefix:* wildcard). Abbreviated syntax must be used, and predicates are not allowed. Thus, person/name/first_name/@id is a legal XPath expression for this attribute, but person//name/@id is not. Several instances of this restricted form of XPath expression can be combined with the vertical bar so that person/name/first_name/@id | person/name/last_name/@id is also an acceptable XPath expression. Finally, the XPath expression may begin with .// so that .//name/@id is legal. However, this is the only place the descendant-or-self axis can be used. No other forms of XPath expression are allowed here.

xs:fractionDigits

<xs:fractionDigits
  fixed = "( true | false )"
  id = "ID"
  value = "nonNegativeInteger" >
  <!-- (xs:annotation?) -->
</xs:fractionDigits>

The xs:fractionDigits facet element is used when deriving from xs:decimal (and its subtypes) by restriction. It limits the number of digits allowed after the decimal point to at most the number specified by the value attribute. This sets only the maximum number of digits after the decimal point. If you want to set the minimum number of digits required, you'll have to use the xs:pattern element instead. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of fractionDigits given here.

xs:group

<xs:group
  name = "NCName"
  ref  = "NCName"
  minOccurs = "nonNegativeInteger"
  maxOccurs = "nonNegativeInteger | unbounded">
  <!-- ( xs:annotation?, (xs:all | xs:choice | xs:sequence) ) -->
</xs:group>

The xs:group element can be used in two ways. As a top-level element with a name attribute, it defines a model group that can be referenced from complex types elsewhere in the schema. The content model of the group is established by a child xs:all, xs:choice, or xs:sequence element.

The second use is inside a xs:complexType element. Here the xs:group element indicates that the contents of the group should appear at this point in the instance document at least as many times as indicated by the minOccurs attribute and at most as many times as indicated by the maxOccurs attribute. The default for both of these is 1. The group to be included is indicated by the ref attribute that contains the name of a top-level xs:group element found elsewhere in the schema.

xs:import

<xs:import
  id = "ID"
  namespace = "anyURI"
  schemaLocation = "anyURI" >
  <!-- ( xs:annotation? ) -->
</xs:import>

Since each schema document has exactly one target namespace, the top-level xs:import element is needed to create schemas for documents that involve multiple namespaces. The namespace attribute contains the namespace URI for the application that the imported schema describes. If the imported schema describes elements and types in no namespace, then the namespace attribute is omitted. The optional schemaLocation attribute contains a relative or absolute URL pointing to the actual location of the schema document to import.

There is no limit to import depth. Schema A can import schema B, which itself imports schema C and schema D. In such a case, schema A can use definitions and declarations from all four schemas. Even recursion (schema A imports schema B, which imports schema A) is not prohibited. Since the imported schema must describe a different target namespace than the importing schema, conflicts between definitions in the multiple schemas are normally not a problem. However, if conflicts do arise, then the schema is in error and cannot be used. There are no precedence rules for choosing between multiple conflicting definitions or declarations.

xs:include

<xs:include
  id = "ID"
  schemaLocation = "anyURI">
  <!-- (annotation?) -->
</xs:include>

The top-level xs:include element is used to divide a schema into multiple separate documents. The schemaLocation attribute contains a relative or absolute URI pointing to the schema document to include. It differs from xs:import in that all included files describe the same target namespace.

There is no limit to inclusion depth. Schema A can include schema B, which itself includes schema C and schema D. In such a case, schema A can use definitions and declarations from all four documents. Even recursion (schema A includes schema B, which includes schema A) is not prohibited, though it is strongly discouraged. Instance documents would refer only to the top-level schema A in their xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute.

Validation is performed after all includes are resolved. If there are any conflicts between the including schema and an included schema--for instance, one schema declares that the FullName element has a simple type, and another declares that the FullName element has a complex type--then the schema is in error and cannot be used. Most of the time schemas should be carefully managed so that each element and type is defined in exactly one schema document.

xs:key

<xs:key
  id = "ID"
  name = "NCName" >
  <!-- (xs:annotation?, (xs:selector, xs:field+) ) -->
</xs:key>

Keys establish uniqueness and co-occurrence constraints among various nodes in the document. For example, you can define a key for an Employee element based on its EmployeeNumber child element and then require that each Assignment element have a team attribute whose contents are a list of employee keys.

The xs:key element defines a new key. It appears only as a child of an xs:element element following the element's type. The name of the key is specified by the name attribute. The elements that have a value for this key are identified by the xs:selector child element. The value of the key for each of these nodes is given by the xs:field child element and must be unique within that set. If there is more than one xs:field child element, then the key has multiple values.

xs:keyref

<xs:keyref
  id = "ID"
  name = "NCName"
  refer = "QName" >
  <!-- (xs:annotation?, (xs:selector, xs:field+) ) -->
</xs:keyref>

The xs:keyref element is placed inside xs:element elements to indicate that a certain part of those elements must match the key with the name given by the refer attribute. The value that is matched against the specified key is determined by the XPath expressions used in the xs:selector child and the xs:field child elements.

xs:length

<xs:length
  fixed = "( true | false )"
  id = "ID"
  value = "nonNegativeInteger" >
  <!-- (xs:annotation?) -->
</xs:length>

The xs:length facet element specifies the exact number of characters in a type derived from xs:string, xs:QName, xs:language, xs:anyURI, or xs:NOTATION. When applied to a list type such as xs:ENTITIES, this facet specifies the number of items in the list. Finally, when applied to xs:hexBinary and xs:base64Binary, it specifies the number of bytes in the decoded data, rather than the number of characters in the encoded data. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of length given here.

xs:list

<xs:list
  id = "ID"
  itemType = "QName" >
  <!-- (xs:annotation?, (xs:simpleType?) ) -->
</xs:list>

The xs:list element is placed inside an xs:simpleType element to derive a new list simple type from a base atomic type identified by the itemType attribute. Alternately, instead of referencing an existing simple type with itemType, a new anonymous atomic type for the list can be created by an xs:simpleType child element. In either case, the newly defined simple type is a whitespace-separated list of atomic-type instances.

xs:maxExclusive

<xs:maxExclusive
  fixed = "( true | false )"
  id = "ID"
  value = "anySimpleType" >
  <!-- (xs:annotation?) -->
</xs:maxExclusive>

The xs:maxExclusive facet element applies to all ordered types, including xs:decimal, xs:float, xs:double, xs:date, xs:duration, xs:dateTime, xs:time, xs:gDay, xs:gMonthYear, xs:gMonth, xs:gYear, and their subtypes. The value attribute contains the maximum value in a form appropriate for the type. For example, the maximum for a type derived from xs:integer might be 75; the maximum for a type derived from xs:double might be 1.61803; and the maximum for a type derived from xs:date might be 2001-09-26. All instances of this type must be strictly less than the maximum value. They may not be equal to the maximum. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of maxExclusive given here.

xs:maxInclusive

<xs:maxInclusive
  fixed = "( true | false )"
  id = "ID"
  value = "anySimpleType" >
  <!-- (xs:annotation?) -->
</xs:maxInclusive>

The xs:maxInclusive facet element applies to all ordered types, including xs:decimal, xs:float, xs:double, xs:date, xs:duration, xs:dateTime, xs:time, xs:gDay, xs:gMonthYear, xs:gMonth, xs:gYear, and their subtypes. The value attribute contains the maximum value in a form appropriate for the type. For example, the maximum for a type derived from xs:integer might be 75; the maximum for a type derived from xs:double might be 1.61803; and the maximum for a type derived from xs:date might be 2001-09-26. All instances of this type must be less than or equal to the maximum value. They may be equal to the maximum. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of maxInclusive given here.

xs:maxLength

<xs:maxLength
  fixed = "( true | false )"
  id = "ID"
  value = "nonNegativeInteger" >
  <!-- (xs:annotation?) -->
</xs:maxLength>

The xs:maxLength facet element specifies the maximum number of characters in a type derived from xs:string, xs:QName, xs:language, xs:anyURI, or xs:NOTATION. It can also be used to restrict xs:hexBinary and xs:base64Binary. However, in this case, it refers to the maximum number of bytes in the decoded data rather than the maximum number of characters in the encoded data. Finally, when applied to a list type such as xs:IDREFS, it describes the maximum number of items in the list. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of maxLength given here.

xs:minExclusive

<xs:minExclusive
  fixed = "( true | false )"
  id = "ID"
  value = "anySimpleType" >
  <!-- (xs:annotation?) -->
</xs:minExclusive>

The xs:minExclusive facet element applies to all ordered types, including xs:decimal, xs:float, xs:double, xs:date, xs:duration, xs:dateTime, xs:time, xs:gDay, xs:gMonthYear, xs:gMonth, xs:gYear, and their subtypes. The value attribute contains the minimum value in a form appropriate for the type. For example, the minimum for a type derived from xs:integer might be 75; the minimum for a type derived from xs:double might be 1.61803; and the minimum for a type derived from xs:date might be 2001-09-26. All instances of this type must be strictly greater than the minimum value. They may not be equal to the minimum. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of minExclusive given here.

xs:minInclusive

<xs:minInclusive
  fixed = "( true | false )"
  id = "ID"
  value = "anySimpleType" >
  <!-- (xs:annotation?) -->
</xs:minInclusive>

The xs:minInclusive facet element applies to all ordered types, including xs:decimal, xs:float, xs:double, xs:date, xs:duration, xs:dateTime, xs:time, xs:gDay, xs:gMonthYear, xs:gMonth, xs:gYear, and their subtypes. The value attribute contains the minimum value in a form appropriate for the type. For example, the minimum for a type derived from xs:integer might be 75; the minimum for a type derived from xs:double might be 1.61803; and the minimum for a type derived from xs:date might be 2001-09-26. All instances of this type must be greater than or equal to the minimum value. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of minInclusive given here.

xs:minLength

<xs:minLength
  fixed = "( true | false )"
  id = "ID"
  value = "nonNegativeInteger" >
  <!-- (xs:annotation?) -->
</xs:minLength>

The xs:minLength facet element specifies the minimum number of characters in a type derived from xs:string, xs:QName, xs:language, xs:anyURI, or xs:NOTATION. It can also be used to restrict xs:hexBinary and xs:base64Binary. However, in this case, it refers to the minimum number of bytes in the decoded data rather than the minimum number of characters in the encoded data. Finally, when applied to a list type such as xs:IDREFS, it describes the minimum number of items in the list. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of maxLength given here.

xs:notation

<xs:notation
  id = "ID"
  name = "NCName"
  public = "PUBLIC identifier"
  system = "anyURI" >
  <!-- (xs:annotation?) -->
</xs:notation>

The top-level xs:notation element defines a notation. It's the schema equivalent of the DTD's <!NOTATION> declaration. Each notation has a name, a public ID, and a system ID identified by the relevant attribute on this element.

xs:pattern

<xs:pattern
  id = "ID"
  value = "regular expression" >
  <!-- (xs:annotation?) -->
</xs:pattern>

The xs:pattern facet element is used to derive new simple types by specifying a regular expression against which values of the type are compared. It applies to all simple types. The schema regular-expression grammar is quite similar to that used in Perl 5.6 and later. (The big change from earlier versions of Perl is support for Unicode character class-based regular expressions.) Most strings and characters match themselves, but a few characters have special meanings, as summarized in Table 21-1. In this table, A and B are subexpressions; n and m are non-negative integers; a, b, c, and d are all single Unicode characters; and X is a name.

Table 21-1. XML Schema regular-expression syntax

Pattern

Matches

(A)

A string that matches A

A | B

A string that matches A or a string that matches B

AB

A string that matches A followed by a string that matches B

A?

Zero or one repetitions of a string that matches A

A*

Zero or more repetitions of a string that matches A

A+

One or more repetitions of a string that matches A

A{n,m}

A sequence of between n and m strings, each of which matches A

A{n}

A sequence of exactly n strings, each of which matches A

A{n,}

A sequence of at least n strings, each of which matches A

[abcd]

Exactly one of the characters listed inside the square brackets

[^abcd]

Exactly one character not listed inside the square brackets

[a-z]

Exactly one character with a Unicode value between a and z, inclusive

[a-z-[d-h]]

Exactly one character included in the outer range but not in the inner range

\n

The newline; &#x0A;

\r

The carriage return; &#x0D;

\t

The tab; &#x09;

\\

The backslash, \

\|

The vertical bar, |

\.

The period, .

\-

The hyphen, -

\^

The caret, ^

\?

The question mark, ?

\*

The asterisk, *

\+

The plus sign, +

\{

The left curly brace, {

\}

The right curly brace, }

\(

The left parenthesis, (

\)

The right parenthesis, )

\[

The left square bracket, [

\]

The right square bracket, ]

.

Any single character except the carriage return or line feed

\s

A space, tab, carriage return, or line feed

\S

Any single character except a space, tab, carriage return, or line feed

\i

An XML name-start character

\c

An XML name character

\d

A decimal digit

\D

Any single character except a decimal digit

\w

A "word character," that is, any single character that is not a punctuation mark, a separator, or "other" (as defined by Unicode)

\W

Any single character that is a punctuation mark, a separator, or "other" (as defined by Unicode)

\p{X}

Any single character from the Unicode character class X; character class names are listed in Table 21-2

\P{X}

Any single character not in the Unicode character class X

\p{IsX}

Any single character from the Unicode character block X. Block names include BasicLatin, Latin-1Supplement, LatinExtended-A, LatinExtended-B, IPAExtensions, SpacingModifierLetters, CombiningDiacriticalMarks, Greek, Cyrillic, Armenian, Hebrew, Arabic, Syriac, Thaana, Devanagari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada, Malayalam, Sinhala, Thai, Lao, Tibetan, Myanmar, Georgian, HangulJamo, Ethiopic, Cherokee, UnifiedCanadianAboriginalSyllabics, Ogham, Runic, Khmer, Mongolian, LatinExtendedAdditional, GreekExtended, GeneralPunctuation, SuperscriptsandSubscripts, CurrencySymbols, CombiningMarksforSymbols, LetterlikeSymbols, NumberForms, Arrows, MathematicalOperators, MiscellaneousTechnical, ControlPictures, OpticalCharacterRecognition, EnclosedAlphanumerics, BoxDrawing, BlockElements, GeometricShapes, MiscellaneousSymbols, Dingbats, BraillePatterns, CJKRadicalsSupplement, KangxiRadicals, IdeographicDescriptionCharacters, CJKSymbolsandPunctuation, Hiragana, Katakana, Bopomofo, HangulCompatibilityJamo, Kanbun, BopomofoExtended, EnclosedCJKLettersandMonths, CJKCompatibility, CJKUnifiedIdeographsExtensionA, CJKUnifiedIdeographs, YiSyllables, YiRadicals, HangulSyllables, HighSurrogates, HighPrivateUseSurrogates, LowSurrogates, PrivateUse, CJKCompatibilityIdeographs, AlphabeticPresentationForms, ArabicPresentationForms-A, CombiningHalfMarks, CJKCompatibilityForms, SmallFormVariants, ArabicPresentationForms-B, Specials, HalfwidthandFullwidthForms, Specials, OldItalic, Gothic, Deseret, ByzantineMusicalSymbols, MusicalSymbols, MathematicalAlphanumericSymbols, CJKUnifiedIdeographsExtensionB, CJKCompatibilityIdeographsSupplement, Tags, and PrivateUse. The characters from many of these blocks are shown in Chapter 26.

\P{IsX}

Any single character not in the Unicode character block X

Table 21-2. Unicode character classes

Unicode character class

Includes

L

Letters

Lu

Uppercase letters

Ll

Lowercase letters

Lt

Titlecase letters

Lm

Modifier letters

Lo

Other letters

M

All marks

Mn

Nonspacing marks

Mc

Spacing combining marks

Me

Enclosing marks

N

Numbers

Nd

Decimal digits

Nl

Number letters

No

Other numbers

P

Punctuation

Pc

Connector punctuation

Pd

Dashes

Ps

Opening punctuation

Pe

Closing punctuation

Pi

Initial quotes

Pf

Final quotes

Po

Other punctuation

Z

Separators

Zs

Spaces

Zl

Line breaks

Zp

Paragraph breaks

S

Symbols

Sm

Mathematical symbols

Sc

Currency symbols

Sk

Modifier symbols

So

Other symbols

C

Other characters (nonletters, nonsymbols, non-numbers, nonseparators)

Cc

Control characters

Cf

Format characters

Co

Private use characters

Cn

Unassigned code points

xs:redefine

<xs:redefine
  id = "ID"
  schemaLocation = "anyURI" >
  <!-- (annotation | (simpleType | complexType | group | attributeGroup))* 
    -->
</xs:redefine>

The xs:redefine element is used much like xs:include. That is, it inserts definitions and declarations for the same target namespace from a schema document found at a URL specified by the schemaLocation attribute. However, unlike xs:include, xs:redefine can override type, model group, and attribute group definitions from the included schema. The new type and group definitions are children of the xs:redefine element. They must extend or restrict the original definition of the redefined type or group. Note, however, that xs:redefine cannot override element and attribute declarations made in the included schema.

xs:restriction

<xs:restriction
  base = "QName"
  id = "ID">
  <!-- ( xs:annotation?, (
        (xs:simpleType?,
        ( xs:minExclusive | xs:minInclusive | xs:maxExclusive
        | xs:maxInclusive | xs:totalDigits | xs:fractionDigits
        | xs:length | xs:minLength | xs:maxLength | xs:enumeration
        | xs:whiteSpace | xs:pattern)*)
        | ( (xs:group | xs:all | xs:choice | xs:sequence)?,
          ((xs:attribute | xs:attributeGroup)*, xs:anyAttribute?) )
       ) -->
</xs:restriction>

The xs:restriction element derives a new type from an existing base type identified by either a base attribute or an xs:simpleType child element. When deriving by restriction, all valid values of the derived type must also be legal values of the base type. However, the reverse is not true. The valid values of the derived type are a subset (almost always a proper subset) of the valid values of the base type. For derived simple types, the allowed values are identified by the various facet child elements of the xs:restriction element. For derived complex types, the allowed values are identified by the same elements you'd find inside an xs:complexType element--that is, zero or one group elements such as xs:all, xs:choice, or xs:sequence followed by attribute representation elements such as xs:attribute, xs:attributeGroup, and xs:anyAttribute.

xs:schema

<xs:schema
  attributeFormDefault = "( qualified | unqualified )"
  elementFormDefault   = "( qualified | unqualified )"
  blockDefault = "( #all | extension | restriction | substitution )
  finalDefault = "( #all | extension | restriction )
  id = "ID"
  targetNamespace = "anyURI"
  version = "token"
  xml:lang = "language" >
  <!-- (
         (xs:include | xs:import | xs:redefine | xs:annotation)*,
         (((xs:simpleType | xs:complexType | xs:group
          | xs:attributeGroup) | xs:element | xs:attribute
          | xs:notation), xs:annotation*)*
   ) -->
</xs:schema>

xs:schema is the root element of all schema documents. It contains all the top-level elements described elsewhere in this chapter. First come all the elements that somehow reference other schema documents, including xs:include, xs:import, and xs:redefine. These are followed by the various elements that define types and groups and declare elements and attributes. As usual, xs:annotation elements can be placed anywhere that is convenient.

Attributes

id, optional
id is an XML name unique within ID-type attributes in this schema document.

targetNamespace, optional
The namespace URI for the XML application described by this schema. If not present, then this schema describes elements in no namespace. If the XML application uses multiple namespaces, then there must be a separate schema document for each different namespace. These schemas can be connected with xs:import elements.

version, optional
You can use this attribute to specify the version of the schema, e.g., 1.0, 1.0.1, 1.1, 1.2, 1.3b1, 2.0, etc. This refers to the version of the specific schema, not the version of the W3C XML Schema Language used in this document.

blockDefault, optional
The blockDefault attribute establishes the default value for the block attributes of xs:element and xs:complexType elements in this schema.

finalDefault, optional
The finalDefault attribute establishes the default value for the final attributes of xs:element and xs:complexType elements in this schema.

xml:lang, optional
This is the human language in which this schema is primarily written, such as en or fr-CA.

attributeFormDefault, optional
This sets the default value for the form attribute of xs:attribute elements. This specifies whether or not locally declared attributes are namespace qualified by the target namespace. If this attribute is not used, locally declared attributes are unqualified unless the form attribute of the xs:attribute element has the value qualified.

elementFormDefault, optional
This sets the default for the form attribute of xs:element elements. This specifies whether locally declared elements are namespace-qualified by the target namespace. By default, locally declared elements are unqualified unless the form attribute of the xs:element element has the value qualified.

WARNING: elementFormDefault is part of a misguided effort to make child elements and attributes equivalent. If you're using namespaces at all, just put all elements in the target namespace of the schema and set elementFormDefault to qualified.

xs:selector

<xs:selector
  id = "ID"
  xpath = "XPath expression" >
  <!-- (xs:annotation?) -->
</xs:selector>

A single xs:selector element is placed inside each xs:unique, xs:key, and xs:keyref element to specify the element nodes for which the key on key reference is defined. The node set is selected by an XPath expression contained in the value of the xpath attribute. The context node for this XPath expression is the element matched by the xs:element declaration in which the xs:unique, xs:key, or xs:keyref element appears.

Not all XPath expressions are allowed here. In particular, the XPath expression must be an abbreviated location path that limits itself to the child axis. The only node tests used are element name, the * wildcard, and the prefix:* wildcard. Abbreviated syntax must be used; predicates are not allowed. Thus, person/name/first_name is a legal XPath expression for this attribute, but person//name and name/first_name/@id are not. Several instances of this restricted form of XPath expression can be combined with the vertical bar so that person/name/first_name | person/name/last_name is also an acceptable XPath expression. Finally, the XPath expression may begin with .// so that .//name is valid. However, this is the only place the descendant-or-self axis can be used. No other forms of XPath expression are allowed here.

xs:sequence

<xs:sequence
  id = "ID"
  maxOccurs = "( nonNegativeInteger | unbounded)"
  minOccurs = "nonNegativeInteger" >
  <!-- (  xs:annotation?,
        ( xs:element | xs:group | xs:choice | xs:sequence | xs:any )*
       )
  -->
</xs:sequence>

The xs:sequence element indicates that the elements represented by its child elements should appear at that position in the instance document in the order they're listed here. The sequence must repeat at least minOccurs times and at most maxOccurs times. The default for both minOccurs and maxOccurs is 1. The maxOccurs attribute can be set to unbounded to indicate that the sequence may repeat indefinitely.

xs:simpleContent

<xs:simpleContent
  id = "ID" >
  <!-- (xs:annotation?, (xs:restriction | xs:extension)) -->
</xs:simpleContent>

The xs:simpleContent element is used inside xs:complexType elements whose content is a simple type, such as xs:string or xs:integer, rather than child elements or mixed content. This is customarily done when the only reason an element has a complex type instead of a simple type is for the purpose of attributes.

xs:simpleType

<xs:simpleType
  final = "( #all | list | union | restriction )"
  id = "ID"
  name = "NCName" >
  <!-- (xs:annotation?, (xs:restriction | xs:list | xs:union)) -->
</xs:simpleType>

The xs:simpleType element defines a new simple type for elements and attributes. A simple type is composed purely of text but no child elements--#PCDATA, in DTD parlance. A top-level xs:simpleType element has a name given in the name attribute by which it can be referred to from the type attribute of xs:element and xs:attribute elements. Alternately, an xs:element or xs:attribute element can have an xs:simpleType child without a name attribute that defines an anonymous type for that element or attribute.

New types are derived from existing types in one of three ways: by restricting the range of a base type using an xs:restriction child element, by combining multiple base types with an xs:union child element, or by allowing multiple values of a base type separated by whitespace with an xs:list child element.

The final attribute can be used to prevent a simple type from being subtyped. If final contains the value list, the type cannot be extended by listing. If final contains the value restriction, the type cannot be extended by restriction. If final contains the value union, the type cannot become a member of a union. These three values can be combined in a whitespace-separated list. For instance, final="list union" prevents derivation by list and union but not by restriction. If final has the value #all, the type cannot be used as a base type in any way.

xs:totalDigits

<xs:totalDigits
  fixed = "( true | false )"
  id = "ID"
  value = "positiveInteger" >
  <!-- (xs:annotation?) -->
</xs:totalDigits>

The xs:totalDigits facet element is used when deriving from xs:decimal elements and its descendants (xs:integer, xs:long, xs:nonNegativeInteger, xs:unsignedLong, etc.) by restriction. It specifies the maximum number of digits allowed in the number, including both the integer and fractional parts, but not counting the decimal point or the sign. This only sets the maximum number of digits. If you want to specify a minimum number of digits, use the xs:pattern element instead. If the fixed attribute has the value true, then types derived from this type are not allowed to override the value of fractionDigits given here.

xs:union

<xs:union
  id = "ID"
  memberTypes = "List of QName" >
  <!-- (xs:annotation?, (xs:simpleType*)) -->
</xs:union>

The xs:union element is placed inside an xs:simpleType to indicate that an element or attribute can contain any one of multiple types. For example, it can say that an element can contain either an xs:integer or an xs:token. The names of the types that participate in the union are listed in the memberTypes attribute separated by whitespace. Furthermore, the types defined in the xs:simpleType children of the xs:union are also members of the union.

xs:unique

<xs:unique
  id = "ID"
  name = "NCName" >
  <!-- (xs:annotation?, xs:selector, xs:field+ ) -->
</xs:unique>

The xs:unique element requires that a specified subset of elements and/or attributes in the instance document have unique values calculated from each of those elements/attributes. This is similar to the constraint imposed by declaring an attribute to have type xs:ID, but much more flexible. The xs:selector child element uses XPath to specify the subset of nodes from the instance document over which uniqueness is calculated. The xs:field children use XPath expressions to specify what properties of those nodes must be unique within the subset.

xs:whiteSpace

<xs:whiteSpace
  fixed = "( true | false )"
  id = "ID"
  value = "(collapse | preserve | replace)" >
  <!-- (xs:annotation?) -->
</xs:whiteSpace>

The xs:whiteSpace facet element is unusual in that it does not constrain values. Instead, it tells the validator how it should normalize whitespace before validating the value against other facets. The value attribute has one of three values:

preserve
All whitespace is significant; this is conceptually similar to the pre element in HTML.

collapse
Before the value is validated, tabs, carriage returns, and line feeds are replaced by spaces; leading and trailing whitespace is deleted; and runs of more than one consecutive space are condensed to a single space.

replace
Tabs, carriage returns, and line feeds are replaced by spaces before the value is validated.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.