XML technologies / XML Schema / Creating complex data types / Mixed content models
(Excerpt from “XML Schema” by Eric van der Vlist)
Although W3C XML Schema allows mixed content models and describes them better than XML DTDs, it treats them as an afterthought grafted onto complex content models. The advantage is that this gives us exactly the same control over child elements as we have just learned for complex content. The disadvantage is that we relinquish all control over child text nodes, which cannot be restricted at all. Child-element descriptions also have exactly the same limitations as complex content models. The limitations of unordered content models are probably even less friendly to the more free-form mixed content models than to complex content models.
Creating mixed content models
This additional construction is implemented using the attribute mixed on xs:complexType, global definition. Otherwise, usage is exactly the same as for complex content models. Setting this attribute to true allows arbitrary text nodes in the content model, before, between and after child elements. The position, whitespace processing and data type of these text nodes cannot be restricted in any way.
Let us return to the definition of our element title and change it to accept a reduced form of XHTML, allowing a to specify a link and the element em to highlight parts of the text. The definition, previously an extension of a simple type into a complex type with simple content, must be rewritten as a complex-content definition with its attribute mixed set to true. The complete definition, including the element a, a complex type markedText and its use to define the element title, could look like this:
<xs:element name="a">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="href" type="xs:anyURI"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:complexType name="markedText" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="em" type="xs:token"/>
<xs:element ref="a"/>
</xs:choice>
<xs:attribute ref="lang"/>
</xs:complexType>
<xs:element name="title" type="markedText"/>This definition matches elements such as:
<title lang="de">
Auf
<em>
den
</em>
<a href="http://dmoz.org/Shopping/Pets/Dogs/">
Hund
</a>
gekommen
</title>The title’s length can no longer be restricted here.
Deriving mixed content models
Mixed content models are derived exactly like the complex content models onto which they are grafted. The semantics of both methods remain unchanged.
Derivation by extension
Complex types with mixed content can be derived by extension from other complex types with complex content, with the same meaning. To add an element to my mixed-content type markedText, namely strong, I can define the following content model:
<xs:element name="title">
<xs:complexType mixed="true">
<xs:complexContent mixed="true">
<xs:extension base="markedText">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="strong" type="xs:string"/>
</xs:choice>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>Note, however, that this extension is equivalent to:
<xs:complexType name="resultingType" mixed="true">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="em" type="xs:token"/>
<xs:element ref="a"/>
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="strong" type="xs:string"/>
</xs:choice>
</xs:sequence>
<xs:attribute ref="lang"/>
</xs:complexType>This is probably not what we want in practice, since this model expects all a and em before the first occurrence of strong. We shall see later, under Building further components with object-oriented design, that this particular problem can be solved with a construction called a substitution group instead of xs:choice.
Derivation by restriction
Mixed content models are also derived by restriction using the method defined for complex content models. The same limitation applies: every particle must explicitly derive from the corresponding particle of the base type. To illustrate the consequences, let us revisit the definition and use of our markedText:
<xs:element name="a">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="href" type="xs:anyURI"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:complexType name="markedText" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="em" type="xs:token"/>
<xs:element ref="a"/>
</xs:choice>
<xs:attribute ref="lang"/>
</xs:complexType>
<xs:element name="title" type="markedText"/>If we want to prohibit em elements in our title, restrict href to absolute HTTP URIs and restrict the attribute lang to the values en and de, we must restructure things to prove that the element a contained in our title is an explicit derivation of the general definition of a. We must also replace the previous anonymous definition of a with a global complex-type definition.
<xs:element name="a" type="link"/>
We can now either derive a new global complex type from the new complex type link or embed its derivation in the definition of the element title.
<xs:element name="title">
<xs:complexType mixed="true">
<xs:complexContent mixed="true">
<xs:restriction base="markedText">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="a">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="link">
<xs:attribute name="href">
<xs:simpleType>
<xs:restriction base="xs:anyURI">
<xs:pattern value="http://.*"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:attribute name="lang">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:enumeration value="en"/>
<xs:enumeration value="de"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>This example is a caricature. In practice, defining a global helper type to avoid nesting several derivations would be more readable, but the example gives an overview of the derivation process.
Derivations between complex and mixed content models
Since complex and mixed content models are built with the same mechanisms, we may ask how complex content can be derived from mixed content and vice versa. The answer lies in the semantics of the two derivation methods.
Derivation by extension appends new content to the base type’s content while preserving its structure. It is therefore impossible to derive a mixed content model from a complex one. In a mixed model, text-node positions cannot be restricted, allowing text nodes anywhere inside the base type. For the same reason, a mixed content model cannot be extended into a complex one, since text nodes allowed in the base type would suddenly be forbidden.
Derivation by restriction defines a subset of the base type. Deriving a mixed content model from a complex one is forbidden: the result would allow text nodes forbidden by the base type, extending rather than restricting its model. But the last remaining combination does work: restricting a mixed model to a complex one. Prohibiting a mixed model’s text nodes is a valid restriction. This is done by setting the attribute mixed in the definition of xs:complexType to false . It is even possible to derive a simple content model from a mixed one, since this is merely a restriction removing sibling nodes while retaining text nodes. Naturally, those siblings must be optional, that is, their attribute minOccurs must have the value 0.
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