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

Defining Named Types

We have seen that we can define elements and attributes as we need them (Russian doll design), or create them first and reference them (flat catalog). W3C XML Schema gives us a third mechanism, which is to define data types (either simple types that will be used for PCDATA elements or attributes or complex types that will be used only for elements) and to use these types to define our attributes and elements.

This is achieved by giving a name to the simpleType and complexType elements, and locating them outside of the definition of elements or attributes. We will also take the opportunity to show how we can derive a datatype from another one by defining a restriction over the values of this datatype.

For instance, to define a datatype named nameType, which is a string with a maximum of 32 characters, we will write:

<xs:simpleType name="nameType">
  <xs:restriction base="xs:string">
    <xs:maxLength value="32"/>
  </xs:restriction>
</xs:simpleType>

The simpleType element holds the name of the new datatype. The restriction element expresses the fact that the datatype is derived from the string datatype of the W3C XML Schema namespace (attribute base) by applying a restriction, i.e. by limiting the number of possible values. The maxLength element that, called a facet, says that this restriction is a condition on the maximum length to be 32 characters.

Another powerful facet is the pattern element, which defines a regular expression that must be matched. For instance, if we do not care about the "-" signs, we can define an ISBN datatype as 10 digits thus:

<xs:simpleType name="isbnType">
  <xs:restriction base="xs:string">
    <xs:pattern value="[0-9]{10}"/>
  </xs:restriction>
</xs:simpleType>

Facets, and the two other ways to derive a datatype (list and union) are covered in the next sections.

Complex types are defined as we've seen before, but given a name.

Defining and using named datatypes is comparable to defining a class and using it to create an object. A datatype is an abstract notion that can be used to define an attribute or an element. The datatype plays then the same role with an attribute or an element that a class would play with an object.

Full listing:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <!-- definition of simple types -->
  <xs:simpleType name="nameType">
    <xs:restriction base="xs:string">
      <xs:maxLength value="32"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="sinceType">
    <xs:restriction base="xs:date"/>
  </xs:simpleType>
  <xs:simpleType name="descType">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
  <xs:simpleType name="isbnType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9]{10}"/>
    </xs:restriction>
  </xs:simpleType>
  <!-- definition of complex types -->
  <xs:complexType name="characterType">
    <xs:sequence>
      <xs:element name="name" type="nameType"/>
      <xs:element name="friend-of" type="nameType" minOccurs="0"
	       maxOccurs="unbounded"/>
      <xs:element name="since" type="sinceType"/>
      <xs:element name="qualification" type="descType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="bookType">
    <xs:sequence>
      <xs:element name="title" type="nameType"/>
      <xs:element name="author" type="nameType"/>
      <xs:element name="character" type="characterType" minOccurs="0"/>
      <!-- the definition of the "character" element is
        using the "characterType" complex type    -->
    </xs:sequence>
    <xs:attribute name="isbn" type="isbnType" use="required"/>
  </xs:complexType>
  <!-- Reference to "bookType" to define the
     "book" element -->
  <xs:element name="book" type="bookType"/>
</xs:schema>

Download this schema: library3.xsd

The next page shows how grouping, compositors and derivation can be used to further promote re-use and structure in schemas.

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

Next Pagearrow