Chapter 21. Schemas Reference

CONTENTS

  •  21.1 The Schema Namespaces
  •  21.2 Schema Elements
  •  21.3 Primitive Types
  •  21.4 Instance Document Attributes

The W3C XML Schema Language (schemas) is a declarative language used to describe the allowed contents of XML documents by assigning types to elements and attributes. The schema language includes several dozen standard types and allows you to define your own custom types. The combination of the information in an XML document instance and the types in that document as defined by the schema is sometimes called the Post Schema Validation Infoset (PSVI).

A schema processor reads both an input XML document and a schema (which is itself an XML document because the W3C XML Schema Language is an XML application) and returns a Boolean result specifying whether the document adheres to the constraints in the schema. A document that satisfies all the schema's constraints and in which all the document's elements and attributes are declared is said to be schema-valid, though in this chapter we will mostly just call such documents valid. A document that does not satisfy all of the constraints is said to be invalid.

21.1 The Schema Namespaces

All standard schema elements are in the http://www.w3.org/2001/XMLSchema namespace. In this chapter, we assume that this URI is mapped to the xs prefix using an appropriate xmlns:xs declaration. This declaration is almost always placed on the root element start-tag:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

In addition, several attributes are used in instance documents to associate schema information with them, including schemaLocation and type. These attributes are in the http://www.w3.org/2001/XMLSchema-instance namespace. In this chapter, we assume that this URI is mapped to the xsi prefix with an appropriate xmlns:xsi declaration on either the element where this attribute appears or one of its ancestors.

In a few cases, schema elements may contain elements from other, arbitrary namespaces or no namespace at all. This occurs primarily inside xs:appinfo and xs:documentation elements, which provide supplementary information about the schema itself or the documents the schema describes to systems that are not schema validators or to people reading the schema.

Finally, most schema elements can have arbitrary attributes from other namespaces. For instance, this allows you to make an xs:attribute element a simple XLink by giving it xlink:type and xlink:href attributes or to identify the language of an xs:notation using an xml:lang attribute. However, this capability is not much used in practice.

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 FirstNames and between one and three LastNames 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.

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.

21.3 Primitive Types

The W3C XML Schema Language provides 44 built-in simple types for text strings. Each type has a value space and a lexical space. The value space is the set of unique meanings for the type, which may or may not be text. In some sense, the value space is composed of Platonic forms. The lexical space is the set of text strings that correspond to particular points in the value space. For example, the xs:boolean type has the value space true and false. However, its lexical space contains four strings: true, false, 0, and 1. true and 1 both map to the same value true, while false and 0 map to the single value false. In cases like this where multiple strings in the lexical space map to a single value, then one of those strings is selected as the canonical lexical representation. For instance, the canonical lexical representations of true and false are the strings true and false.

The primitive types are organized in a hierarchy. All simple types descend from an abstract ur-type called xs:anySimpleType, which is itself a descendant of an abstract ur-type called xs:anyType that includes both simple and complex types. Simple types are derived from other simple types by union, restriction, or listing. For example, the xs:nonNegativeInteger type is derived from the xs:integer type by setting its minInclusive facet to 0. The xs:integer type is derived from the xs:decimal type by setting its fractionDigits facet to 0. Figure 21-1 diagrams the complete hierarchy of built-in types. The xs:simpleType element allows you to apply facets to these types to create your own derived types that extend this hierarchy.

Figure 21-1. The simple type hierarchy

figs/xian2_2101.gif

The types are organized in the following section alphabetically. For each type the value and lexical spaces are described, and some examples of permissible instances are provided.

xs:anyURI  

   

The xs:anyURI type indicates a Uniform Resource Identifier. This includes not only Uniform Resource Locators (URLs), but also Uniform Resource Names (URNs). Both relative and absolute URLs are allowed. Legal xs:anyURI values include the following:

  • http://www.cafeaulait.org/

  • http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]/

  • http://www.w3.org/TR/xmlschema-2/#anyURI

  • #xpointer(/book/chapter[20]/sect1[2]/sect2[1])

  • gopher://spinaltap.micro.umn.edu/00/Weather/

  • mailto:elharo@metalab.unc.edu

  • chapters/ch03.html

  • http://ibiblio.org/nywc/compositions.phtml?category=Concertos

More specifically, elements of this type must be composed exclusively of the ASCII letters A-Z and a-z and digits 0-9, as well as the ASCII punctuation marks -, _, ., !, ~, *, ', (, and ). In addition, the ASCII punctuation marks ;, /, ?, :, @, &, =, +, $, %, and , may be used for their intended purposes in URLs, e.g., the forward slash can be used as the path separator but not as part of a filename. All other characters must be escaped by encoding each byte of their UTF-8 representation as a percent sign followed by the hexadecimal value of the character. Although there are other restrictions on what does and does not make a legal URI, in practice the only conditions that schema processors check are the limitations on the characters that may appear.

Constraining facets that apply to xs:anyURI are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:base64Binary  

   

The xs:base64Binary type represents an arbitrary sequence of bytes that has been encoded in ASCII characters using the Base-64 algorithm defined in RFC 2045, Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies. The data is divided into lines of 76 characters or less. The letters A-Z and a-z, the digits 0-9, and the punctuation marks + and / are used to encode data. The equals sign is use to pad data at the end if necessary. Other characters are allowed, but are ignored by the Base-64 decoder.

The constraining facets that apply to xs:base64Binary are length, minLength, maxLength, pattern, enumeration, and whiteSpace. Unlike string types, the values specified by the length, minLength, and maxLength facets refer to the number of bytes in the decoded data, not to the number of characters in the encoded data.

xs:boolean  

   

The xs:boolean type represents a logical Boolean whose value is either true or false. There are exactly four legal values for elements and attributes whose type is Boolean:

  • true

  • false

  • 0

  • 1

0is the same as false, and 1 is the same as true. Only two constraining facets apply to xs:boolean: pattern and whiteSpace.

xs:byte  

   

The xs:byte type represents an integer with a value between -128 and 127. It is a subtype of the xs:short type. Legal values include any sequence of digits whose value is less than or equal to 127 and greater than or equal to -128. An optional leading plus or minus sign is allowed. For example, these are legal bytes:

  • 127

  • -128

  • 0

  • 52

  • +52

  • 0000052

Constraining facets that apply to xs:byte are length, minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:date  

   

The xs:date type represents a specific day in history such as September 26, 2001. Dates are written in the form CCYY-MM-DD. For example, September 26, 2001 is written as 2001-09-26. Dates in the far future and distant past can be written with more than four digits in the year, but at least four digits are required. Dates before year 1 are written with a preceding minus sign. (There was no year 0.) An optional time zone indicator in the form hh:mm may be suffixed to provide a time zone as an offset from Coordinated Universal Time (Greenwich Mean Time, UTC). For example, 2001-09-26-05:00 is September 26, 2001 in the U.S. Eastern time zone. A Z can be used instead to indicate UTC. These are all valid values of type xs:date:

  • 2001-01-01

  • 1999-12-31Z

  • 0482-11-24

  • -0052-10-23

  • 2002-12-23+12:00

  • 87500-01-01

Constraining facets that apply to xs:date are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. Note, however, that when the time zone is not specified, it's not always possible to determine unambiguously whether one date begins after another.

xs:dateTime  

   

The xs:dateTime type represents a specific moment in history, such as 3:32 P.M., September 26, 2001. Dates are written in the form CCYY-MM-DDThh:mm:ss. For example, 3:32 P.M., September 26, 2001 is written as 2001-09-26T15:32:00. Decimal fractions of a second can be indicated by appending a period and any number of digits after the seconds. Dates in the far future and distant past can be written with more than four digits in the year, but at least four digits are required. Dates before year 1 are written with a preceding minus sign. (There was no year 0.) An optional time zone indicator in the form hh:mm may be suffixed to provide a time zone as an offset from Coordinated Universal Time (Greenwich Mean Time, UTC). For example, 2001-09-26T15:32:00-05:00 is 3:32 P.M., September 26, 2001 in the U.S. Eastern time zone. A Z can be used instead to indicate UTC. These are all valid values of type xs:dateTime:

  • 2001-01-01T03:32:00-05:00

  • 1999-12-31T00:00:00Z

  • 2002-12-23T17:08:30.121893632178

Constraining facets that apply to xs:dateTime are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. Note, however, that when the time zone is not specified, it's not always possible to determine unambiguously whether one time falls after another.

xs:decimal  

   

xs:decimal is the base type for all numeric built-in schema types, except xs:float and xs:double. It represents a base 10 number with any finite number of the digits 0-9 before and after the decimal point. It may be prefixed with either a plus sign or a minus sign. These are all valid values of type xs:decimal:

  • 3.1415292

  • 03.1415292

  • 127

  • +127

  • -128

  • 0.0

  • 0.

  • .0

This type is not conducive to localization. Only European digits can be used, and only a period can be used as a decimal point. Exponential and scientific notation are not supported.

Constraining facets that apply to xs:decimal are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, fractionDigits, and totalDigits.

xs:double  

   

The xs:double type is designed to represent eight-byte, binary floating-point numbers in IEEE 754 format, such as is used by the double type in Java and many C compilers. This includes the special values INF for infinity and NaN for not a number, used for the results of unconventional operations like dividing by zero and taking the square root of a negative number. Because not all binary numbers can be precisely represented by decimal numbers, it is possible that two different decimal representations in the lexical space map to the same value (and vice versa). In this case, the closest approximation IEEE-754 value is chosen. These are all legal values of type xs:double:

  • 3.1415292

  • -03.1415292

  • 6.022E23

  • 127E-13

  • +2.998E+10

  • -128e12

  • 0.0

  • INF

  • NaN

  • -INF

Constraining facets that apply to xs:double are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace.

xs:duration  

   

The xs:duration type represents a length of time such as 15 minutes; 2 hours; or 3 years, 7 months, 2 days, 8 hours, 32 minutes, and 12 seconds. It does not have a specific beginning or end, just a length. Durations are represented using the ISO-8601 standard format PnYnMnDTnHnMnS. nY gives the number of years, nM the number of months, nD the number of days, nH the number of hours, nM the number of minutes, and nS the number of seconds. The number of years, months, days, hours, minutes, and seconds are all given as non-negative integers. The number of seconds is a decimal number with as many places after the decimal point as necessary. For example, in this format, 3 years, 7 months, 2 days, 8 hours, 32 minutes, and 12 seconds is written as P3Y7M2DT8H32M12S. Any values that are zero can be omitted. Thus, a duration of 2 years and 2 minutes can be written as P2YT2M. If there are no hours, minutes, or seconds, then the T is omitted. Thus, a duration of two years is written as P2Y. A leading minus sign before the P indicates a negative duration.

Constraining facets that apply to xs:duration are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. However, because the number of days in a month varies from 28 to 31 and the number of days in a year varies from 365 to 366, durations are not always perfectly ordered. For instance, whether P1M is greater than, equal to, or less than P30D depends on which month it is.

xs:ENTITIES  

   

The xs:ENTITIES type indicates that the value is a whitespace-separated list of unparsed entity names declared in the instance document's DTD. This is the same as the DTD ENTITIES attribute type.

Constraining facets that apply to xs:ENTITIES are length, minLength, maxLength, enumeration, pattern, and whiteSpace. The length, minLength, and maxLength facets all refer to the number of entity names in the list.

xs:ENTITY  

   

The xs:ENTITY type is a subtype of xs:NCNAME with the additional restriction that the value be declared as an unparsed entity in the document's DTD. The legal lexical values of type xs:ENTITY are exactly the same as for xs:NCNAME. Constraining facets that apply to xs:ENTITY are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

A schema cannot declare either parsed or unparsed entities. An XML document that uses any entities other than the five predefined ones must have a DOCTYPE declaration and a DTD.

xs:float  

   

The xs:float type represents four-byte, binary floating-point numbers in IEEE-754 format, such as is the float type in Java and many C compilers. This includes the special values INF for infinity and NaN for not a number, used for the results of unconventional operations like dividing by zero and taking the square root of a negative number. Because not all binary numbers can be precisely represented by decimal numbers, it is possible that two different decimal representations in the lexical space map to the same value (and vice versa). In this case, the closest approximation of the IEEE-754 value is chosen. These are all legal values of type xs:float:

  • 3.1415292

  • -03.1415292

  • 6.022E23

  • 127E-13

  • +2.998E+10

  • -128e12

  • 0.0

  • INF

  • NaN

  • -INF

Constraining facets that apply to xs:float are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace.

xs:gDay  

   

The xs:gDay type represents a certain day of the month such as the 14th or the 23rd in no particular month. The format used is -- -DD plus an optional time zone suffix in the form hh:mm or Z to indicate Coordinated Universal Time (UTC). These are all valid xs:gDay values:

  • ---01

  • ---28

  • ---29Z

  • ---31+02:00

  • ---15-11:00

The g indicates that the day is given in the Gregorian calendar. Schema date types are not localizable to non-Gregorian calendars. If you need a different calendar, you'll need to derive from xs:string using the pattern facet.

Constraining facets that apply to xs:gDay are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. However, if the time zones are not specified it may not be possible to conclusively determine whether one day is greater than or less than another. If time zones are specified, days are compared by when they start. Thus ---29-05:00 is greater than ---29Z, which is greater than ---29+02:00.

xs:gMonth  

   

The xs:gMonth type represents a certain month of the year in the Gregorian calendar as an integer between --01-- and --12--. An optional time zone suffix in the form hh:mm or Z to indicate Coordinated Universal Time (UTC) can be added as well. These are all valid xs:gMonth values:

  • --01--

  • --12--

  • --12--Z

  • --09--+02:00

  • --03---11:00

XML Schema Part 2: Datatypes has a mistake here. The date time data types are supposed to be in the format specified by the ISO-8601 standard, according to which the proper format for a month is an integer between --01 and --12 with no trailing hyphens. Exactly how this will be fixed remains to be determined, though at the least months of the form --01 and --10 will be allowed in the future.

The g indicates that the month is given using the Gregorian calendar. Schema date types are not localizable to non-Gregorian calendars. If you need a different calendar, you'll need to derive from xs:string using the pattern facet.

Constraining facets that apply to xs:gMonth are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. However, if the time zones are not specified, it may not be possible to determine conclusively whether one month starts before another. If time zones are specified, months are compared by their first moment. Thus, --12---05:00 is greater than --12--Z, which is greater than --12--+02:00.

xs:gMonthDay  

   

The xs:gMonthDay type represents a certain day of a certain month in no particular year. It is written in the format -- MM-DD plus an optional time zone suffix in the form hh:mm or Z to indicate Coordinated Universal Time (UTC). These are all valid xs:gMonthDay values:

  • --10-31

  • --12-25Z

  • --01-01+05:00

  • --07-04-02:00

The g indicates that the month and day are specified in the Gregorian calendar. Schema date types are not localizable to non-Gregorian calendars. For a different calendar, you'll have to derive from xs:string using the pattern facet.

Constraining facets that apply to xs:gMonthDay are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. However, if the time zones are not specified, it is not always possible to determine conclusively whether one day starts before another. If time zones are specified, days are compared by their first moment in the same year.

xs:gYear  

   

The xs:gYear type represents a year in the Gregorian calendar. It is written in the format CCYY plus an optional time zone suffix in the form hh:mm or Z to indicate Coordinated Universal Time (UTC). Dates before year 1 can be indicated by a minus sign. At least four digits are used, but additional digits can be added to indicate years after 9999 or before 9999 BCE. These are all valid xs:gYear values in their order of occurrence:

  • -15000000000

  • 0004

  • 0600

  • 1492

  • 2002+10:00

  • 2002Z

  • 2002-04:30

  • 100000

  • 800000000

Constraining facets that apply to xs:gYear are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. However, if the time zones are not specified, it may not be possible to determine conclusively whether one year starts before another. If time zones are specified, years are compared by their first moment.

xs:gYearMonth  

   

The xs:gYearMonth type represents a month and year in the Gregorian calendar, such as March, 2002. It is written in the format CCYY-MM plus an optional time zone suffix in the form hh:mm or Z to indicate Coordinated Universal Time (UTC). Dates before year 1 can be indicated by a minus sign. At least four digits are used, but additional digits can be added to indicate years after 9999 or before 9999 BCE. These are all valid xs:gYearMonth values in their order of occurrence:

  • -15000000000-05

  • 0004-04

  • 0600-10

  • 1492-11

  • 2002-03+10:00

  • 2002-03Z

  • 2002-03-04:30

  • 100000-07

  • 100000-08

Constraining facets that apply to xs:gYearMonth are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. However, if the time zones are not specified it may not be possible to determine conclusively whether one month and year starts before another.

xs:hexBinary  

   

The xs:hexBinary type represents an arbitrary sequence of bytes that has been encoded by replacing each byte of data with two hexadecimal digits from 0 through F (A is 10, B is 11, C is 12, etc.). Either upper- or lowercase letters may be used in whatever character set the document is written. In UTF-8 or ASCII, this has the effect of exactly doubling the space used for the data.

The constraining facets that apply to xs:hexBinary are length, minLength, maxLength, pattern, enumeration, and whiteSpace. Unlike string types, the values specified by the length, minLength, and maxLength facets refer to the number of bytes in the decoded data, not to the number of characters in the encoded data.

xs:ID  

   

xs:ID is a subtype of xs:NCName with the additional restriction that the value is unique among other items of type xs:ID within the same document. The legal lexical values of type xs:ID are exactly the same as for xs:NCName. Constraining facets that apply to xs:ID are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:IDREF  

   

xs:IDREF is a subtype of xs:NCName with the additional restriction that the value is used elsewhere in the instance document on an item with type xs:ID. The legal lexical values of type xs:ID are exactly the same as for xs:NCName. Constraining facets that apply to xs:IDREF are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:IDREFS  

   

The xs:IDREFS type indicates that the value is a whitespace-separated list of xs:ID type values used elsewhere in the instance document. This is the same as the DTD IDREFS attribute type.

Constraining facets that apply to xs:IDREFS are length, minLength, maxLength, enumeration, pattern, and whiteSpace. The length, minLength, and maxLength facets all refer to the number of IDREFs in the list.

xs:int  

   

The xs:int type represents a signed integer small enough to be represented as a four-byte, two's complement number such as Java's int primitive data type. It is derived from xs:long by setting the maxInclusive facet to 2147483647 and the minInclusive facet to -2147483648. These are all legal values of type xs:int:

  • 200

  • 200000

  • -200000

  • +2147483647

  • -2147483648

  • 0

Constraining facets that apply to xs:int are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:integer  

   

The xs:integer type represents a mathematical integer of arbitrary size. The type is derived from xs:double by fixing the fractionDigits facet at 0. It may be prefixed with either a plus sign or a minus sign. If no sign is present, a plus is assumed. These are all legal values of type xs:integer:

  • 3

  • 3000

  • 349847329847983264983264987326487326487324678346374

  • +127

  • -128

  • 0

  • +0

  • -0

Constraining facets that apply to xs:integer are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:language  

   

Elements and attributes with type xs:language contain a language code as defined in RFC 1766, Tags for the Identification of Languages. These are essentially the acceptable values for the xml:lang attribute described in Chapter 5. If possible, this should be one of the two-letter language codes defined in ISO 639, possibly followed by a country code. For languages that aren't listed in ISO 639, you can use one of the i-codes registered with IANA. If the language you need isn't present in either of these sets, you can make up your own language tag beginning with the prefix "x-" or "X-". Thus, these are acceptable language values:

  • en

  • en-US

  • en-GB

  • fr-CA

  • i-klingon

  • x-quenya

  • X-PigLatin

Constraining facets that apply to xs:language are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:long  

   

The xs:long type represents a signed integer that can be represented as an eight-byte, two's complement number such as Java's long primitive data type. It is derived from xs:integer by setting the maxInclusive facet to 9223372036854775807 and the minInclusive facet to -9223372036854775808. These are all legal values of type xs:long:

  • 2

  • 200

  • +9223372036854775807

  • -9223372036854775808

  • 5000000000

  • 0

Constraining facets that apply to xs:long are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:Name  

   

xs:Name is a subtype of xs:token that is restricted to legal XML 1.0 names. In other words, the value must consist exclusively of letters, digits, ideographs, and the underscore, hyphen, period, and colon. Digits, the hyphen, and the period may not be used to start a name, though they may be used inside the name. These are all legal values of type xs:Name:

  • G127

  • _128

  • Limit

  • xml-stylesheet

  • svg:rect

  • figs/p407.gif

  • figs/u4eb0.gif

Constraining facets that apply to xs:Name are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:NCName  

   

An xs:NCName is a noncolonized name as defined in Namespaces in XML. This is a legal XML name that does not contain a colon. The value must consist exclusively of letters, digits, ideographs, and the underscore, hyphen, and period. Digits, the hyphen, and the period may not be used to start a name, though they may be used inside the name. These are all legal values of type xs:NCName:

  • I-10

  • _128

  • Limit

  • xml-stylesheet

  • figs/u30a8.giffigs/u30ea.giffigs/u30aa.giffigs/u30c3.giffigs/u30c8.gif

Constraining facets that apply to xs:NCName are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:negativeInteger  

   

The xs:negativeInteger type represents a mathematical integer that is strictly less than zero. It is derived from xs:integer by setting the maxInclusive facet to -1. These are all legal values of type xs:negativeInteger:

  • -2

  • -200

  • -9223372036854775809

  • -9223372036854775808922337203685477580892233720368

  • -34

Constraining facets that apply to xs:negativeInteger are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:NMTOKEN  

   

An xs:NMTOKEN is the schema equivalent of the DTD NMTOKEN attribute type. It is a subtype of xs:token that is restricted to legal XML 1.0 name tokens. These are the same as XML names except that there are no restrictions on what characters may be used to start the name token. In other words, the value must consist of one or more letters, digits, ideographs, and the underscore, hyphen, period, and colon. These are all legal values of type xs:NMTOKEN:

  • 127

  • -128

  • Limit

  • integration

  • svg:rect

  • figs/u03c0.giffigs/u03c5.giffigs/u03c1.giffigs/u03b3.giffigs/u03bf.giffigs/u03b4.giffigs/u03b1.giffigs/u03b9.giffigs/u03ba.giffigs/u03c4.giffigs/u03bf.giffigs/u03c2.gif

  • figs/u4e7f.gif

Constraining facets that apply to xs:NMTOKEN are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:NMTOKENS  

   

The xs:NMTOKENS type is the schema equivalent of the DTD NMTOKENS attribute type. xs:NMTOKENS is derived from xs:NMTOKEN by list. Thus, a value of type xs:NMTOKENS contains one or more whitespace-separated name tokens. These are all legal values of type xs:NMTOKENS:

  • 127 126 125 124 123 122 121 120 119 118

  • -128

  • Limit Integral Sum Sup Liminf Limsup

  • Jan Feb Mar Apr May June July Sept Nov Dec

  • svg:rect

Constraining facets that apply to xs:NMTOKENS are length, minLength, maxLength, enumeration, pattern, and whiteSpace. The length, minLength, and maxLength facets all refer to the number of name tokens in the list.

xs:nonNegativeInteger  

   

The xs:nonNegativeInteger type represents a mathematical integer that is greater than or equal to zero. It is derived from xs:integer by setting the minInclusive facet to 0. These are all legal values of type xs:nonNegativeInteger:

  • 2

  • +200

  • 9223372036854775809

  • 9223372036854775808922337203685477580892233720368

  • 0

Constraining facets that apply to xs:nonNegativeInteger are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:nonPositiveInteger  

   

The xs:nonPositiveInteger type represents a mathematical integer that is less than or equal to zero. It is derived from xs:integer by setting the maxInclusive facet to 0. These are all legal values of type xs:nonPositiveInteger:

  • -2

  • -200

  • -9223372036854775809

  • -9223372036854775808922337203685477580892233720368

  • 0

Constraining facets that apply to xs:nonPositiveInteger are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:normalizedString  

   

xs:normalizedString is derived from xs:string by setting the whiteSpace facet to replace so that the carriage return (#xD) and tab (#x9) characters are replaced by spaces in the normalized value. A normalized string can contain any characters that are allowed in XML, though depending on context, special characters such as <, &, and " may have to be escaped with character or entity references in the usual way. All legal strings are also legal lexical representations of type xs:normalizedString. However, a schema-aware parser that presents the normalized value of an element instead of the literal characters in the document will replace all carriage returns and tabs with spaces before passing the string to the client application.

Constraining facets that apply to xs:normalizedString are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:NOTATION  

   

The xs:NOTATION type restricts a value to those qualified names declared as notations using an xs:notation element in the schema. This is an abstract type. In other words, you cannot directly declare that an element or attribute has type xs:NOTATION. Instead, you must first derive a new type from xs:NOTATION, most commonly by enumeration, and then declare that your element or attribute possesses the subtype. Constraining facets that apply to xs:NOTATION are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:positiveInteger  

   

The xs:positiveInteger type represents a mathematical integer that is strictly greater than zero. It is derived from xs:integer by setting the minInclusive facet to 1. These are all legal values of type xs:positiveInteger:

  • 1

  • +2

  • 9223372036854775809

  • 9223372036854775808922337203685477580892233720368

  • 34

Constraining facets that apply to xs:positiveInteger are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:QName  

   

An xs:QName is a base type that is restricted to namespace-qualified names. The logical value of a qualified name is a namespace URI, local part pair. Lexically, qualified names are the same as XML names except that they may not contain more than one colon and that colon may not be the first character in the name. A qualified name may or may not be prefixed. If it is prefixed, then the prefix must be properly mapped to a namespace URI. If it is not prefixed, then the name must occur in the scope of a default namespace. These are all legal values of type xs:QName provided that this condition is met in context:

  • xsl:apply-templates

  • svg:rect

  • limit

  • xml:lang

  • body

  • xlink:href

Constraining facets that apply to xs:QName are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:short  

   

The xs:short type indicates a signed integer small enough to be represented as a two-byte, two's complement number such as Java's short primitive data type. It is derived from xs:int by setting the maxInclusive facet to 32767 and the minInclusive facet to -32768. These are all legal values of type xs:int:

  • 2000

  • +2000

  • -2000

  • 32767

  • -32768

  • 0

Constraining facets that apply to xs:short are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:string  

   

This is the most general simple type. Elements and attributes with type xs:string can contain any sequence of characters allowed in XML, though depending on context certain characters such as <, &, and " may have to be escaped with character or entity references in the usual way.

Constraining facets that apply to xs:string are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:time  

   

The xs:time type represents a specific time of day on no particular day, such as 3:32 P.M. Times are written in the form hh:mm:ss.xxx using a 24-hour clock and as many fractions of a second as necessary. For example, 3:41 P.M. is written as 15:41:00. 3:41 A.M. and a half-second is written as 03:41:00.5. The Z suffix indicates Coordinated Universal Time (Greenwich Mean Time, UTC). Otherwise, the time zone can be indicated as an offset in hours and minutes from UTC. For example, 15:41:00-05:00 is 3:41 P.M., in the U.S. Eastern time zone. The time zone may be omitted, in which case the actual time is somewhat nondeterministic. These are all valid values of type xs:time:

  • 03:32:00-05:00

  • 00:00:00Z

  • 08:30:34.121893632178

  • 23:59:59

Constraining facets that apply to xs:time are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whiteSpace. Note, however, that when the time zone is not specified, it's not always possible to determine unambiguously whether one time falls after another.

xs:token  

   

xs:token is a subtype of xs:normalizedString whose normalized value does not contain any line feed (#xA) or tab (#x9) characters, does not have any leading or trailing whitespace, and has no sequence of two or more spaces. All legal strings are also legal lexical representations of type xs:normalizedString. However, a schema-aware parser that presents the normalized value of an element instead of the literal characters in the document will trim leading and trailing whitespace and compress all others runs of whitespace characters with a single space before passing the string to the client application.

Constraining facets that apply to xs:token are length, minLength, maxLength, pattern, enumeration, and whiteSpace.

xs:unsignedByte  

   

The xs:unsignedByte type represents a non-negative integer that can be stored in one byte, such as the unsigned char type used by some (but not all) C compilers. It is derived from xs:unsignedShort by setting the maxInclusive facet to 255 (28-1). These are all legal values of type xs:unsignedByte:

  • 3

  • 200

  • +255

  • 50

  • 0

Constraining facets that apply to xs:unsignedByte are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:unsignedInt  

   

The xs:unsignedInt type represents a non-negative integer that can be stored in four bytes, such as the unsigned int type used by some C compilers. It is derived from xs:unsignedLong by setting the maxInclusive facet to 4294967295 (2321-1). These are all legal values of type xs:unsignedInt:

  • 2

  • 200

  • +4294967295

  • 100000

  • 0

Constraining facets that apply to xs:unsignedInt are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:unsignedLong  

   

The xs:unsignedLong type represents a non-negative integer that can be stored in eight bytes, such as the unsigned long type used by some C compilers. It is derived from xs:nonNegativeInteger by setting the maxInclusive facet to 18446744073709551615 (264-1). These are all legal values of type xs:unsignedLong:

  • 2

  • 200

  • +9223372036854775807

  • 18446744073709551615

  • 5000000000

  • 0

Constraining facets that apply to xs:unsignedLong are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

xs:unsignedShort  

   

The xs:unsignedShort type represents a non-negative integer that can be stored in four bytes, such as the unsigned short type used by some C compilers. It is derived from xs:unsignedInt by setting the maxInclusive facet to 65535 (216-1). These are all legal values of type xs:unsignedShort:

  • 3

  • 300

  • +65535

  • 50000

  • 0

Constraining facets that apply to xs:unsignedShort are minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whiteSpace, and totalDigits.

21.4 Instance Document Attributes

The W3C XML Schema Language defines four attributes in the http://www.w3.org/2001/XMLSchema-instance namespace (here mapped to the xsi prefix), which are attached to elements in the instance document rather than elements in the schema. These are as follows: xsi:nil, xsi:type, xsi:schemaLocation, and xsi:noNamespaceSchemaLocation. All four of these attributes are special in that the schemas do not need to declare them.

xsi:nil  

   

The xsi:nil attribute indicates that a certain element does not have a value or that the value is unknown. This is not the same as having a value that is zero or the empty string. Semantically, it is equivalent to SQL's null. For example, in this full_name element, the last_name child has a nil value:

<full_name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">   <first_name>Cher</first_name>   <last_name xsi:nil="true"/> </full_name>

It is not relevant whether an empty-element tag or a start-tag/end-tag pair is used to represent the nil element. However, a nil element may not have any content.

In order for this document to be valid, the element declaration for the name element must explicitly specify that nil values are allowed by setting the nillable attribute to true. For example:

<xs:element name="last_name" type="xs:string" nillable="true"/>
xsi:noNamespaceSchemaLocation  

   

The xsi:noNamespaceSchemaLocation attribute locates the schema for elements that are not in any namespace. (Attributes that are not in any namespace are assumed to be declared in the same schema as their parent element.) Its value is a relative or absolute URL where the schema document can be found. It is most commonly attached to the root element but can appear further down the tree. For example, this person element claims that it should be validated against the schema found at the URL http://www.elharo.com/person.xs:

<person xsi:noNamespaceSchemaLocation="http://www.elharo.com/person.xs">   <name>     <first_name>Alan</first_name>     <last_name>Turing</last_name>   </name>   <profession>computer scientist</profession>   <profession>mathematician</profession>   <profession>cryptographer</profession> </person>

These are only suggestions. Schema processors may use other means of locating the relevant schemas and to ignore the hints provided by xsi:noNamespaceSchemaLocation.

xsi:schemaLocation  

   

The xsi:schemaLocation attribute locates schemas for elements and attributes that are in a specified namespace. Its value is a namespace URI followed by a relative or absolute URL where the schema for that namespace can be found. It is most commonly attached to the root element but can appear further down the tree. For example, this person element in the http://www.cafeconleche.org/namespaces/person namespace claims that it should be validated against the schema found at the URL http://www.elharo.com/person.xs:

<person xmlns="http://www.cafeconleche.org/namespaces/person"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.cafeconleche.org/namespaces/person                       http://www.elharo.com/person.xs">   <name>     <first_name>Alan</first_name>     <last_name>Turing</last_name>   </name>   <profession>computer scientist</profession>   <profession>mathematician</profession>   <profession>cryptographer</profession> </person>

If more than one namespace is used in a document, then each namespace must have its own schema. The namespace URIs and schema URLs can be listed in sequence in the same xsi:schemaLocation attribute. For example, the xsi:schemaLocation attribute on this person element says that items from the http://www.cafeconleche.org/namespaces/person namespace should be validated against the schema found at the URL http://www.elharo.com/person.xs, while items from the http://www.cafeconleche.org/namespaces/names namespace should be validated against the schema found at the relative URL names.xs.

<person xmlns="http://www.cafeconleche.org/namespaces/person"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.cafeconleche.org/namespaces/person                       http://www.elharo.com/person.xs                       http://www.cafeconleche.org/namespaces/names                       names.xs">   <name xmlns="http://www.cafeconleche.org/namespaces/names">     <first_name>Alan</first_name>     <last_name>Turing</last_name>   </name>   <profession>computer scientist</profession>   <profession>mathematician</profession>   <profession>cryptographer</profession> </person>

These are only suggestions. Schema processors are allowed to use other means of locating the relevant schemas and to ignore the hints provided by xsi:schemaLocation.

xsi:type  

   

The xsi:type attribute may be used in instance documents to indicate the type of an element, even when a full schema is not available. For example, this length element has type xs:decimal:

<length xsi:type="xs:decimal">23.5</length>

More importantly, the xsi:type attribute enables a limited form of polymorphism. That is, it allows you to place an instance of a derived type where an instance of the base type would normally be expected. The instance of the derived type must carry an xsi:type attribute identifying it as an instance of the base type. For example, suppose a schema says that a transport element must contain exactly one vehicle element. If bus, train, and airplane are all subtypes of vehicle, then these are valid transport elements:

<transport>   <airplane xsi:type="airplane">Boeing 767</airplane> </transport> <transport>   <bus xsi:type="bus">Greyhound</bus> </transport>

However, when the xsi:type attributes are removed, these are no longer valid transport elements:

<transport>   <airplane>Boeing 767</airplane> </transport> <transport>   <bus>Greyhound</bus> </transport>

 

CONTENTS


XML in a Nutshell
XML in a Nutshell, 2nd Edition
ISBN: 0596002920
EAN: 2147483647
Year: 2001
Pages: 28

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net