XML technologies / XML Schema / Creating complex data types / Complex content models
(Excerpt from “XML Schema” by Eric van der Vlist)
Restricting and extending simple content models is useful, but XML has little use without more complex models.
Creating complex content
Complex content specifies its elements and attributes and their order. We have seen local complex-type examples in Using and developing schemas and Our first schema:
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
These show the basic structure of complex-type definitions with complex content. Element xs:complexType contains the definition, here local: xs:complexType is not at top level but inside an element xs:element , making it anonymous. Beneath xs:complexType we find the child-element sequence, xs:sequence, and the attribute list.
Compositors and particles
Here, elements xs:sequence act as compositors, while elements xs:element within xs:sequence act as particles. Other compositors and particles extend this basic scenario.
W3C XML Schema defines three compositors: xs:sequence for ordered particle lists, xs:choice for choosing one of several particles, and xs:all for unordered particle lists. Compositors xs:sequence and xs:choice can themselves specify occurrences through attributes minOccurs and maxOccurs and act as particles. Significant restrictions apply to xs:all, which cannot be a particle, as the next section shows.
The particles are xs:element, xs:sequence, xs:choice and also xs:any and xs:group, introduced later. Nesting compositors is key to complex structures, but suffers from W3C XML Schema and its allergy to nondeterminism.
For an example, suppose library names may be either an element name as before, or three separate elements for first, optional middle and last names. These allow three combinations:
<first-name>
Charles
</first-name>
<middle-name>
M.
</middle-name>
<last-name>
Schulz
</last-name>
<first-name>
or:
<first-name>
Peppermint
</first-name>
<last-name>
Patty
</last-name>
or:
<name>
Snoopy
</name>
Replace the reference to element name with a choice of an element name or a sequence of first-name, middle-name, optional, and last-name. The definition of author becomes:
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element ref="name"/>
<xs:sequence>
<xs:element ref="first-name"/>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>Element name also occurs inside element character. Copying the structure xs:choice there would work, but let us introduce a convenient construction for reusable element sets instead.
Element and attribute groups
Element and attribute groups contain sets of elements or attributes handled as a whole. These simple, flexible structures define content fragments reusable in several places, such as our name structure xs:choice.
First define a named global group, directly under element xs:schema:
<xs:group name="name">
<xs:choice>
<xs:element ref="name"/>
<xs:sequence>
<xs:element ref="first-name"/>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice>
</xs:group>
Group references then serve as particles in compositors:
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:group ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:sequence>
<xs:group ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>Attribute groups similarly use xs:attributeGroup:
<xs:attributeGroup name="bookAttributes">
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
</xs:attributeGroup>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
<xs:attributeGroup ref="bookAttributes"/>
</xs:complexType>
</xs:element>The unique particle attribution rule, UPA
Another example illustrates one of the strictest limitations of W3C XML Schema. To describe every book page, we want different elements, odd-page and even-page, for odd and even pages with different pagination. We might try this group:
<xs:group name="pages">
<xs:sequence>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element ref="odd-page"/>
<xs:element ref="even-page"/>
</xs:sequence>
<xs:element ref="odd-page" minOccurs="0"/>
</xs:sequence>
</xs:group>This seems a simple, clever sequence of odd/even pairs with a possible final odd page. It covers even- and odd-length books and single-page brochures. Neither XSV nor Xerces appreciates it:
XSV:
vdv@evlist:~/w3c-xml-schema/user/examples/complex-types$ xsd -n first-ambigous.xsd first-ambigous.xml
using xsv (default)
<?xml version='1.0'?>
<xsv docElt='{None}library' instanceAssessed='true' instanceErrors='0'
rootType='[Anonymous]' schemaDocs='first-ambigous.xsd' schemaErrors='1'
target='/home/vdv/w3c-xml-schema/user/examples/complex-types/first-ambigous.xml'
validation='strict' version='XSV 1.203.2.20/1.106.2.11 of 2001/11/01 17:07:43'
xmlns='http://www.w3.org/2000/05/xsv'>
<schemaDocAttempt URI='/home/vdv/w3c-xml-schema/user/examples/complex-types/first-ambigous.xsd'
outcome='success' source='command line'/>
<schemaError char='7' line='65' phase='instance'
resource='file:///home/vdv/w3c-xml-schema/user/examples/complex-types/first-ambigous.xsd'>
non-deterministic content model for type None: {None}:odd-page/{None}:odd-page</schemaError>
</xsv>Xerces:
vdv@evlist:~/w3c-xml-schema/user/examples/complex-types$ xsd -n first-ambigous.xsd
-p xerces-cvs first-ambigous.xml
using xerces-cvs
startDocument
[Error] first-ambigous.xml:2:10: Error: cos-nonambig: (,odd-page)
and (,odd-page) violate the "Unique Particle Attribution" rule.
endDocumentSeduced by compositor/particle flexibility, we broke an ancient taboo, known in SGML as ambiguous content models, imported into XML DTDs as nondeterministic content models and retained by W3C XML Schema as the unique particle attribution rule, UPA.
This greatly complicates writing W3C XML Schemas: the rule must hold after all definitions, redefinitions, derivations, imports, references and substitutions are processed. The Recommendation acknowledges the difficulty of succinctly describing it with substitution groups and wildcards. Ultimately, looking only at the current element must never leave a processor unsure of its branch in the definition. In our minimal example, on encountering the first element odd-page, the processor cannot know whether an element even-page follows without looking ahead. This violates UPA.
Adapted from a chessboard-description example, this is a famous inherently nondeterministic model. Many other nondeterministic models can be rewritten deterministically. Distinguish essential from accidental nondeterminism. Return to our two-model name sequence and replace element first-name with name. The model is either name or a sequence of name, middle-name and last-name:
<xs:group name="name">
<xs:choice>
<xs:element ref="name"/>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice>
</xs:group>
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:group ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>On encountering an element name, the processor again needs lookahead to choose a branch. But here the common element name simplifies the model: a required element name optionally followed by a sequence of optional middle-name and required last-name can be written deterministically as:
<xs:group name="name">
<xs:sequence>
<xs:element ref="name"/>
<xs:sequence minOccurs="0">
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:sequence>
</xs:group>This slippery path depends on subtle model details, making maintenance difficult and requiring imperfect compromises. If element name becomes optional in the second branch, the new model is:
<xs:group name="name">
<xs:choice>
<xs:element ref="name"/>
<xs:sequence>
<xs:element ref="name" minOccurs="0"/>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice>
</xs:group>It is nondeterministic for the same reason, but examining its possible combinations yields:
<xs:group name="name">
<xs:choice>
<xs:sequence>
<xs:element ref="name"/>
<xs:sequence minOccurs="0">
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:sequence>
<xs:sequence>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:choice>
</xs:group>Formal theories and algorithms can rewrite nondeterministic models whenever possible. Hopefully, development tools for W3C XML Schema will use them to suggest alternatives to authors.
Ambiguous models were controversial in the 1990s SGML community. The restriction remained in XML DTDs as nondeterministic models, despite opposition from influential XML Special Interest Group members Tim Bray, Jean Paoli and Peter Sharpe. The group wanted to retain compatibility with SGML parsers. Keeping the rule in W3C XML Schema facilitates simple processor implementations such as finite state machines, whose runtime can grow exponentially without UPA. Joe English, James Clark and Murata Makoto strongly criticized this, demonstrating simple alternatives with linear processing even without the rule. This is a major expressive difference between RELAX, TREX and RELAX NG, which lack this rule, and W3C XML Schema.
Consistent declaration rule
Though technically unrelated, consistent declaration and UPA are often associated because violating the former often also violates UPA. The new rule is much easier to explain: W3C XML Schema explicitly forbids choices between same-named elements of different types, as shown:
<xs:choice>
<xs:element name="name" type="xs:string"/>
<xs:element name="name">
<xs:complexType>
<xs:sequence>
<xs:element ref="first-name"/>
<xs:element ref="middle-name"/>
<xs:element ref="last-name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>In Referencing schemas and schema data types in XML documents we shall see how the attribute xsi:type can work around this for some applications.
Unordered content model limitations
Useful as they are, unordered models have limitations.
Limitations of xs:all
Unordered models impose no child order but raise nondeterminism risks and processor complexity. For simpler implementations, the Recommendation severely restricts element xs:all , leaving it barely practical. xs:all cannot be a particle, only a compositor; xs:all may occur at most once; all particles in xs:all must be of type xs:element and occur at most once themselves.
Suppose we make document authors’ lives easier with a vocabulary ignoring child order. For our simple vocabulary, applications would face little difficulty: why require a book title after its ISBN, or authors before characters? The first affected model could be element book:
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="id"/>
<xs:attribute ref="available"/>
</xs:complexType>
</xs:element>Unfortunately, xs:sequence cannot be replaced by xs:all here, since two children, author and character, have unbounded occurrences, exceeding one. The next candidates are author and character, with similar models:
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
Here, both author and character meet the criteria for xs:all, so we can write:
<xs:element name="author">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:all>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:all>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>We have two elements, author and character, whose child order is irrelevant. But this freedom does not consistently cover the whole schema. More importantly, flexibility and extensibility are lost when using an xs:all-compositor. Since each child can occur at most once, element qualification cannot hold several descriptions in different languages. Since particles in xs:all cannot be compositors or groups, we cannot extend the model to accept both name and the sequence first-name, middle-name and last-name .
Since xs:all is rather ineffective generally, some workarounds may suit order-independent vocabularies.
Adapting document structure
If designing a vocabulary from scratch, adapt its structure to the limitations of xs:all. Add a container whenever needing xs:choice, xs:sequence or elements occurring more than once. Containers authors and characters can wrap repeated author and character elements, giving instances such as:
<?xml version="1.0"?>
<library>
<book id="b3810518883" available="true">
<title lang="de">
Auf den Hund gekommen
</title>
<isbn>
3810518883
</isbn>
<authors>
<author id="CMS">
<born>
1922-11-26
</born>
<dead>
2000-02-12
</dead>
<name>
Charles M. Schulz
</name>
</author>
</authors>
<characters>
<character id="PP">
<name>
Peppermint Patty
</name>
<qualification>
kühn, dreist und draufgängerisch
</qualification>
<born>
1966-08-22
</born>
</character>
<character id="Snoopy">
<born>
1950-10-04
</born>
<name>
Snoopy
</name>
<qualification>
extrovertierter Beagle
</qualification>
</character>
<character id="Schroeder">
<qualification>
brachte die klassische Musik in die Peanuts-Comics ein
</qualification>
<name>
Schroeder
</name>
<born>
1951-05-30
</born>
</character>
<character id="Lucy">
<name>
Lucy
</name>
<born>
1952-03-03
</born>
<qualification>
herrschsüchtig, kratzbürstig und egoistisch
</qualification>
</character>
</characters>
</book>
</library>
A complete schema describing this instance is:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:token"/>
<xs:element name="qualification" type="xs:token"/>
<xs:element name="born" type="xs:date"/>
<xs:element name="dead" type="xs:date"/>
<xs:element name="isbn" type="xs:NMTOKEN"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:token">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:all>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:all>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="authors"/>
<xs:element ref="characters"/>
</xs:all>
<xs:attribute ref="id"/>
<xs:attribute ref="available"/>
</xs:complexType>
</xs:element>
<xs:element name="characters">
<xs:complexType>
<xs:sequence>
<xs:element ref="character" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:all>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
</xs:schema>
Our alternative name model makes this more painful. An element xs:choice cannot occur in compositor xs:all, so insert an invariant container level followed by another containing only the choice. Instances become:
<?xml version="1.0"?>
<library>
<book id="b3810518883" available="true">
<title lang="de">
Auf den Hund gekommen
</title>
<isbn>
3810518883
</isbn>
<authors>
<author id="CMS">
<born>
1922-11-26
</born>
<dead>
2000-02-12
</dead>
<name>
<complex-name>
<last-name>
Schulz
</last-name>
<first-name>
Charles
</first-name>
<middle-name>
M.
</middle-name>
</complex-name>
</name>
</author>
</authors>
<characters>
<character id="PP">
<name>
<complex-name>
<first-name>
Peppermint
</first-name>
<last-name>
Patty
</last-name>
</complex-name>
</name>
<qualification>
kühn, dreist und draufgängerisch
</qualification>
<born>
1966-08-22
</born>
</character>
<character id="Snoopy">
<born>
1950-10-04
</born>
<name>
<simple-name>
Snoopy
</simple-name>
</name>
<qualification>
extrovertierter Beagle
</qualification>
</character>
<character id="Schroeder">
<qualification>
brachte die klassische Musik in die Peanuts-Comics ein
</qualification>
<name>
<simple-name>
Schroeder
</simple-name>
</name>
<born>
1951-05-30
</born>
</character>
<character id="Lucy">
<name>
<simple-name>
Lucy
</simple-name>
</name>
<born>
1952-03-03
</born>
<qualification>
herrschsüchtig, kratzbürstig und egoistisch
</qualification>
</character>
</characters>
</book>
</library>
The schema adaptation follows naturally, keeping a flat design:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="simple-name" type="xs:token"/>
<xs:element name="first-name" type="xs:token"/>
<xs:element name="middle-name" type="xs:token"/>
<xs:element name="last-name" type="xs:token"/>
<xs:element name="qualification" type="xs:token"/>
<xs:element name="born" type="xs:date"/>
<xs:element name="dead" type="xs:date"/>
<xs:element name="isbn" type="xs:NMTOKEN"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="available" type="xs:boolean"/>
<xs:attribute name="lang" type="xs:language"/>
<xs:element name="name">
<xs:complexType>
<xs:choice>
<xs:element ref="simple-name"/>
<xs:element ref="complex-name"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:element name="complex-name">
<xs:complexType>
<xs:all>
<xs:element ref="first-name"/>
<xs:element ref="middle-name" minOccurs="0"/>
<xs:element ref="last-name"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="title">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:token">
<xs:attribute ref="lang"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="author">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:all>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:all>
<xs:element ref="isbn"/>
<xs:element ref="title"/>
<xs:element ref="authors"/>
<xs:element ref="characters"/>
</xs:all>
<xs:attribute ref="id"/>
<xs:attribute ref="available"/>
</xs:complexType>
</xs:element>
<xs:element name="characters">
<xs:complexType>
<xs:sequence>
<xs:element ref="character" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:all>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:all>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
</xs:schemaThis can be generalized beyond the limitations of xs:all . We have externalized complexity: formerly hidden in the schema, the full model structure is now in the instance. Choices and sequences, including implicit sequences of repeated elements, become containers. This clearer structure may improve readability; some authors recommend such containers.
xs:choise instead of xs:all Using
If adapting the structure to xs:all is impossible or impractical, replace compositors xs:all by xs:choice where possible. Less general than restructuring, this surprising interchangeability works only with loose occurrence controls, such as a container accepting author and character in any order and number:
<xs:element name="persons">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="author"/>
<xs:element ref="character"/>
</xs:choice>
</xs:complexType>
</xs:element>This means the same as the forbidden definition using xs:all:
<xs:element name="persons">
<xs:complexType>
<xs:all>
<xs:element ref="author" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="character" minOccurs="0"
maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>
</xs:element>Deriving complex content
Complex content derives by extension or restriction from complex types. These methods differ strongly and are asymmetric. Restriction reduces matching instances: every structure matching the restricted type must match the base. Extension adds particles to the model; base-type content need not match the extension. There is generally no round trip: neither restricted nor extended types can generally be extended or restricted back to the base.
Derivation by extension
Extension resembles simple-content complex-type extension and combining groups to create a new complex type. It adds elements and attributes after those in the base, essentially a sequence of old and new models. In our library, elements author and character have similar models: author expects name, born and dead, while for character name, born and qualification are expected. A base type can hold the initial elements shared by both models:
<xs:complexType name="basePerson">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>Extension now appends new elements, dead for author and qualification for character, after those in the base:
<xs:element name="author">
<xs:complexType>
<xs:complexContent>
<xs:extension base="basePerson">
<xs:sequence>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:complexContent>
<xs:extension base="basePerson">
<xs:sequence>
<xs:element ref="qualification"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>Technically, this is a sequence containing the base compositor followed by the compositor in element xs:extension. The models therefore resemble:
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
</xs:sequence>
<xs:sequence>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
</xs:sequence>
<xs:sequence>
<xs:element ref="qualification"/>
</xs:sequence>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
</xs:element>This illustrates extension’s nature: it does not expand accepted instance structures. An element character requiring a child qualification cannot have a valid content model basePerson; it merges two models. The insertion point is fixed: the new compositor follows the base’s. Had shared elements name and born not been first, extension would have been impossible.
We cannot choose the merging compositor either. Extending models using xs:choice does not add choices, but places choices in an xs:sequence. For example, extend the model of element persons from above, defined as a global complex type:
<xs:complexType name="basePersons">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="author"/>
<xs:element ref="character"/>
</xs:choice>
</xs:complexType>Add a new element by extension:
<xs:complexType name="persons">
<xs:complexContent>
<xs:extension base="basePersons">
<xs:sequence>
<xs:element name="editor" type="xs:token" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>The result is equivalent to:
<xs:complexType name="personsEquivalent">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="author"/>
<xs:element ref="character"/>
</xs:choice>
<xs:sequence>
<xs:element name="editor" type="xs:token" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:sequence>
</xs:complexType>Extending the xs:choice as follows is impossible:
<xs:complexType name="personsAsWeWouldHaveLiked">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="author"/>
<xs:element ref="character"/>
<xs:element name="editor" type="xs:token"/>
</xs:choice>
</xs:complexType>For xs:all it is worse: restrictions on composing xs:all still apply. No content can be added to a complex type defined with an xs:all, though attributes can. A compositor xs:all can appear in an extension only if the base content model is empty.
Derivation by restriction
Extension merges models using compositor xs:sequence outside a group; restriction reduces matching instance structures. This resembles restriction of simple data types or simple-content complex types, although facets such as xs:whitespace can expand matching simple-type instances. That is the only similarity. Confusingly, W3C XML Schema uses the same word and element name for distinct meanings and different models in elements xs:restriction.
Unlike simple types, complex types have no facets. Derivation defines the full derived model as a logical restriction: every valid derived structure must also be valid for the base. The specification of W3C XML Schema instead gives a nearly equivalent algorithm for processors.
Restriction declares the derived type a subset of the base. Unlike simple-type derivation, this declaration is needed for substitution and redefinition, as seen in Building blocks and Building further components with object-oriented design, potentially helping applications. With simple types we add facets without knowing those already applied; here we must define the whole model. Attributes are an exception: prohibited removes them, as for simple-content complex types.
Seek a base from which elements author and character can derive by restriction. One must exist: all complex types derive from an abstract xs:anyType accepting arbitrary elements and attributes. In practice, choose the tightest useful base. Since elements name and born occur in author and character with identical counts, retain them unchanged. Elements dead and qualification occur in only one of author and character. Both author and character must match the base, so include both children but make them optional, setting attribute minOccurs to 0. The base is:
<xs:complexType name="person">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
<xs:element ref="qualification" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
Derivations define models within an element xs:restriction. Unchanged attribute declarations are not repeated.
<xs:element name="author">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="person">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="character">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="person">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>Restriction is more verbose than defining the model directly. It does not build modular schemas; it tells applications the models share features. An application knowing complex type person can also handle author and character.
Besides occurrence changes, restriction can reduce valid structures by using a stricter simple type or fixing a value. Each derived particle must explicitly derive from its base counterpart. This limits restriction depth in one step: deeper restrictions may require converting local definitions to global ones. A concrete example is given in “Creating mixed content models”, whose models behave similarly here.
Asymmetry of the methods
We can now revisit the methods’ asymmetry. It is not inherently a flaw, but examining it clarifies their meanings. Consider extending element character from basePerson:
<xs:complexType name="basePerson">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
<xs:element name="character">
<xs:complexType>
<xs:complexContent>
<xs:extension base="basePerson">
<xs:sequence>
<xs:element ref="qualification"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>The model of character requires an element qualification. Valid instances of character are invalid for basePerson and vice versa. Restricting character back to a type basePerson is impossible: restriction requires all derived structures to remain valid for the base.
Now revisit restricting element character from base person :
<xs:complexType name="person">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="dead" minOccurs="0"/>
<xs:element ref="qualification" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="id"/>
</xs:complexType>
<xs:element name="character">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="person">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="born"/>
<xs:element ref="qualification"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>Again, converting the complex type character back into person is impossible: it requires changing the minimum occurrence of qualification from 1 to 0 and inserting optional dead between born and qualification. Neither is allowed by extension, which appends content only: it cannot modify an existing particle’s occurrence count or insert one between existing particles.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de