XML technologies / XML Schema / Creating complex data types / Empty content models
(Excerpt from “XML Schema” by Eric van der Vlist)
Empty content models are elements accepting only attributes. W3C XML Schema provides no special support: these can be complex content without elements or simple content restricted to the empty string.
Creating empty content models
W3C XML Schema treats empty content as the intersection of complex models with no compositors and simple models expecting no text nodes, which W3C XML Schema treats as an empty text node. Either method can create empty content. When extending element title to allow mixed content, we avoided empty elements such as HTML img or br. Let us define a br element with attributes id and class using both methods.
As simple content
Define a simple type accepting only the empty string. Strictly, empty content permits no whitespace between start and end tags. To control this, use a type that leaves whitespace unchanged, xs:string. Derive the empty content model by extending it.
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
<xs:element name="br">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="empty">
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="class" type="xs:NMTOKEN"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>As complex content
The more direct alternative creates complex content without any children.
<xs:element name="br">
<xs:complexType>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="class" type="xs:NMTOKEN"/>
</xs:complexType>
</xs:element>Deriving from empty content
Both forms retain their content models’ derivation methods. Their main difference concerns which derivations apply and with what effects.
Derivation by extension
Both simple and complex content extension can add attributes. But only complex content can gain child elements; simple content cannot change its text-node type. Thus an empty model based on simple content can gain only attributes, while one based on complex content can gain elements.
Derivation by restriction
Restriction seems similar: both forms restrict attributes, while restricting already empty content seems uninteresting. But recall the simple-type facet that enlarges valid instances. Our empty simple type has base xs:string. Deriving from it with xs:whitespace may enlarge permitted instances. Here, setting xs:whitespace to “collapse” accepts any whitespace sequence between tags. This is not strictly empty, but useful to applications normalizing whitespace and not distinguishing the cases. Derive the complex type with simple content as follows:
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="emptyBr">
<xs:simpleContent>
<xs:extension base="empty">
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="class" type="xs:NMTOKEN"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="almostEmptyBr">
<xs:simpleContent>
<xs:restriction base="emptyBr">
<xs:whiteSpace value="collapse"/>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="class" type="xs:NMTOKEN"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>Simple or complex models for empty content?
The choice mainly affects extensibility. To retain the option of adding child elements by derivation, choose empty complex content. To allow whitespace in a derived type, empty simple content is better.
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