XML technologies / XML Schema / Adding depth to our first schema / New lessons

(Excerpt from “XML Schema” by Eric van der Vlist)

Although this schema describes the same document as the schema in The first schema, it highlights quite different aspects of W3C XML Schema .

Depth versus modularity?

Even though the following pages will show how the constructions xs:complexType and xs:group can offset this effect, we have sacrificed our first schema’s modularity for the depth and structure of the second. This is a general tendency in W3C XML Schema .

In practice, you will probably seek a compromise between these styles, allowing some depth below several global elements.

There are, however, two cases where these styles are not equally suitable. The first is when elements with the same name but different content must be defined in different places. Local element definitions should then be used, at least everywhere except one location, since elements are identified by name.

In our example, the element name appears inside author and character with the same data type. We may wish to define the element name inside author and character with different content models, as shown in this instance document:

<?xml version="1.0"?>
<library>
   <book id="b3810518883" available="true">
      <isbn>
         3810518883
      </isbn>
      <title lang="de">
         Auf den Hund gekommen
      </title>
      <author id="CMS">
         <name>
            <first>
               Charles
            </first>
            <middle>
               M.
            </middle>
            <last>
               Schulz
            </last>
         </name>
         <born>
            1922-11-26
         </born>
         <dead>
            2000-02-12
         </dead>
      </author>
      <character id="Snoopy">
         <name>
            Snoopy
         </name>
         <born>
            1950-10-04
         </born>
         <qualification>
            extrovertierter Beagle
         </qualification>
      </character>
   </book>
</library>

Since we can define only one global element named name, at least one of these elements must be defined locally below its parent.

The W3C schema for XML Schema provides several examples of elements whose types depend on where they occur. The next section applies this in our Russian doll schema: global element definitions have a different type in the schema for schemas from local definitions or references, even when they use the same element name, xs:element.

Whether defining same-named elements with different content is good practice is debatable. It may confuse human authors and be hard to document, but W3C Schema’s local definitions prevent confusion for applications processing these documents. Our example has two occurrences of an element named name below author and character respectively. We can readily define different restrictions or even different content for them. Although this could be described as overloaded element names, “character/name” and “author/name”, I find it unreliable because we often lack a clear, simple way to determine the context.

The other case is a recursive schema, where an element can occur directly or indirectly, inside a child, within an element of the same type. A flat design with references is required because recursive structures have unbounded depth.

W3C XML Schema provides several such examples, with local element definitions that can be nested recursively, as in our second schema. A flat design is needed because these elements must be referenced unless we want to limit the structure’s maximum depth; the schema for schemas uses a reference mechanism. Here that mechanism is an element group, a building block not yet introduced, equivalent to an actual element reference.

Russian doll and object-oriented design

Defining elements and attributes locally is often called Russian doll design, because each element’s definition is embedded in its parent’s definition, like Russian dolls nested inside one another.

Seen through an object-oriented lens, the Russian doll design creates objects locally where needed, rather than creating them globally and then cloning them locally as required, as our first schema did.

We still need to learn how to create types, the counterparts of object and container classes. This will let us manipulate sets of objects.

Where have element types gone?

Readers familiar with XML or SGML and DTDs are accustomed to identifying elements by the term “element type”. The XML 1.0 Recommendation states that each element has a type identified by its name. The namespaces specification clarifies this further: an XML namespace is a collection of names, identified by a URI reference [RFC2396], used in XML documents for element types and attribute names.

A surprising feature of our Russian doll schema is that this fundamental concept of element type has completely disappeared: there is no way to determine the element type of name. Two distinct elements have been defined, both named name. Their independent definitions happen to be identical here but could differ—for example, if authors’ first and last names were specified separately but fictional characters’ names were not. The element type name means nothing unless its context is specified.

This loss matters so little that few have noticed it. However, some situations require us to identify elements, such as documenting XML vocabularies. A practical way to write a vocabulary reference is to list all elements with their definitions. This becomes much harder without a clear mapping between element types and their definitions or content models.

RDF is another application based on element types. It uses element types to represent elements as objects in its triples. The element “name” in the namespace "http://dyomedea.com/ns" is identified by "http://dyomedea.com/ns#name". Breaking the connection between element types and schema definitions makes it difficult or impossible to answer basic questions, such as the content model of "http://dyomedea.com/ns#name" or where the element is defined.

I faced this problem while writing this book’s reference section, because the W3C XML Schema for W3C XML Schema uses many local element definitions. I concluded that allowing the same element type, such as xs:restriction, introduced later, to have different content models with different semantics makes the language much harder to understand and schemas much harder to read.

   

 

 

 

back to top