Using W3C XML Schema - Part 2
Advanced W3C XML Schema
This is the second part of our comphrensive tutorial and reference on W3C XML Schemas. If you have not already read the first installment of Using XML Schemas, we advise you to do so before reading this article.
Content Types
In the first part of this series we examined the default content type behavior, modeled after data-oriented documents, where complex type elements are element and attribute only, and simple type elements are character data without attributes.
The W3C XML Schema Definition Language also supports the definition of empty content elements, and simple content elements (those that contain only character data) with attributes.
|
Table of Contents |
|
Content Types |
Empty content elements are defined using a regular
xsd:complexType construct and by purposefully omitting the
definition of a child element. The following construct defines an empty
book element accepting an isbn attribute:
<xsd:element name="book"> <xsd:complexType> <xsd:attribute name="isbn" type="isbnType"/> </xsd:complexType> </xsd:element>
Simple content elements, i.e. character data elements with attributes,
can be derived from simple types using xsd:simpleContent. The
book element defined above can thus be extended to accept a text value
using:
<xsd:element name="book">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="isbn" type="isbnType"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
Note the location of the attribute definition, showing that the extension is achieved through the addition of the attribute. This definition will accept the following XML element:
<book isbn="0836217462"> Funny book by Charles M. Schulz. Its title (Being a Dog Is a Full-Time Job) says it all ! </book>
W3C XML Schema supports mixed content though the mixed attribute
in the xsd:complexType element. Consider
<xsd:element name="book"> <xsd:complexType mixed="true"> <xsd:all> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> </xsd:all> <xsd:attribute name="isbn" type="xsd:string"/> </xsd:complexType> </xsd:element>
which will validate an XML element such as
<book isbn="0836217462"> Funny book by <author>Charles M. Schulz</author>. Its title (<title>Being a Dog Is a Full-Time Job</title>) says it all ! </book>
Unlike DTDs, W3C XML Schema mixed content doesn't modify the constraints on the sub-elements, which can be expressed in the same way as simple content models. While this is a significant improvement over XML 1.0 DTDs, note that the values of the character data, and its location relative to the child elements, cannot be constrained.