XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Using W3C XML Schema
by Eric van der Vlist | Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9

Table of Contents

Introducing Our First Schema
Slicing the Schema
Defining Named Types
Groups, Compositors and Derivation
Content Types
Constraints
Building Usable and Reusable Schemas
Namespaces
W3C XML Schema and Instance Documents
W3C XML Schema Datatypes Reference
W3C XML Schema Structures Reference

Content Types

In the first part of this article, 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 defining empty content elements and simple content (those that contain only character data) with attributes.

Empty content elements are defined using a regular xs:complexType construct and purposefully omitting to define a child element. The following construct defines an empty book element accepting an isbn attribute:

  <xs:element name="book">
    <xs:complexType>
      <xs:attribute name="isbn" type="isbnType"/>
    </xs:complexType>
  </xs:element>

Simple content elements, i.e. character data elements with attributes, can be derived from simple types using xs:simpleContent. The book element defined above can thus be extended to accept a text value by:

  <xs:element name="book">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="isbn" type="isbnType"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

Note the location of the attribute definition, showing that the extension is done 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 xs:complexType elements. Consider

<xs:element name="book">
 <xs:complexType mixed="true">
  <xs:all>
   <xs:element name="title" type="xs:string"/>
   <xs:element name="author" type="xs:string"/>
  </xs:all>
  <xs:attribute name="isbn" type="xs:string"/>
 </xs:complexType>
</xs: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.

Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9

Next Pagearrow