7.4.2.2. Derivation by restriction
Whereas derivation by extension is similar to
merging two content models through a xs:sequence
compositor, derivation by restriction is a restriction of the number
of instance structures matching the complex type. In this respect, it
is similar to the derivation by restriction of simple datatypes or
simple content complex types (even though we've seen
that a facet such as xs:whiteSpace
expanded the number of instance documents matching a simple type).
Note that this is the only similarity between derivations by
restriction of simple and complex datatypes. This is highly
confusing, since W3C XML Schema uses the same word and even the same
element name in both cases, but these words have a different meaning
and the content models of the xs:restriction elements
are different.
Unlike simple type derivation, there are no facets to apply to
complex types, and the derivation is done by defining the full
content model of the derived datatype, which must be a logical
restriction of the base type. Any instance structure valid per the
derived datatype must also be valid per the base datatype. The W3C
XML Schema specification does not define the derivation by
restriction in these terms, but defines a formal algorithm to be
followed by schema processors, which is roughly equivalent.
The derivation by restriction of a complex type is a declaration of
intention that the derived type is a subset of the base type. (Rather
than a derivation we've seen for simple types, this
declaration is needed for features allowing substitutions and
redefinitions of types, which we will see in Chapter 8, "Creating Building Blocks" and Chapter 12, "Creating More Building Blocks Using Object-Oriented Features" and which may
provide useful information used by some applications.) When we derive
simple types, we can take a base type without having to care about
the details of the facets that are already applied, and just add our
own set of facets. Here, on the contrary, we need to provide a full
definition of a content model, except for attributes that can be
declared as "prohibited" to be
excluded from the restriction, something we have seen for the
restriction of complex types with simple contents.
Moving on, let's try to find a base from which we
can derive both the author and
character elements by restriction. This time, we
can be sure that such a complex type exists since all the complex
types can be derived from an abstract xs:anyType,
allowing any elements and attributes. In practice, however, we will
try to find the most restrictive base type that can accommodate our
needs. Since the name and born
elements are present in both author and
character, with the same number of occurrences, we
can keep them as they appear. We then have two elements
(dead and qualification, which
appear only in one of the two elements author and
character). Since both author
and character will need to be valid per the base
type, we will take both of them in the base type but make them
optional by giving them a minOccurs attribute
equal to 0. Our base type can then be:
<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>
The derivations are then done by defining the content model within a
xs:restriction element (note that we have not
repeated the attribute declarations which are not modified):
<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>
We see here that the syntax of a derivation by restriction is more
verbose than the syntax of the straight definition of the content
model. The purpose of this derivation is not to build modular
schemas, but rather to give applications that use this schema the
indication that there is some commonality between the content models,
and if they know how to handle the complex type
"person," they can handle the
elements author and character.
We will see W3C XML Schema features that rely on this derivation
method in Chapter 8, "Creating Building Blocks" and Chapter 12, "Creating More Building Blocks Using Object-Oriented Features".
Changing the number of occurrences of particles is not the only
modification that can be done during a derivation by restriction.
Other operations that result in a reduction of the number of valid
instance structures are also possible, such as changing a simple type
to a more restrictive one or fixing values. The main constraint in
this mechanism is that each particle of the derived type must be an
explicit derivation of the corresponding particle of the base type.
The effect of this statement is to limit the
"depth" of the restrictions that
can be performed in a single step, and when we need to restrict
particles at a deeper level of imbrication, we may have to transform
local definitions into global ones. We will see a concrete example in
Section 7.5.1, "Creating Mixed Content Models", which are similar in this
respect.