home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeXML in a NutshellSearch this book

21.2. Schema Elements

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

One root element
xs:schema

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

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

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

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

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

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

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

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

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

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

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

<!-- ( xs:annotation?, xs:element* ) -->
xs: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: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: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: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: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: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.

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:restriction

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

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

xs:schema

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

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

Attributes

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

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

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

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

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

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

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

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

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

xs: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: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.



Library Navigation Links

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