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

advertisement

Relax NG, Compared
by Eric van der Vlist | Pages: 1, 2, 3, 4, 5

Namespaces

[ Corresponding chapter for W3C XML Schema]

Table of Contents

Introducing Our First Schema

Slicing the Schema

Defining Named Types

Groups, Compositors and Derivation

Content Types

Building Usable -- and Reusable -- Schemas

Namespaces

There is no necessity to define one and only one namespace per schema with RELAX NG. Several namespaces may be defined in a single schema, and, conversely, a namespace may be described by several schemas. The namespace of the elements and attributes being described can be indicated using either an ns attribute or by using qualified names.

The ns attribute is inherited by all the child elements of the element where it is defined. The simplest way to change our first schema to place it in the http://example.org/book namespace would just be to add the ns attribute in the document element:

<?xml version="1.0" encoding="UTF-8"?>
<grammar 
  xmlns="http://relaxng.org/ns/structure/1.0"
  ns="http://example.org/book">
 <start>
  <element name="book">
   <attribute name="isbn">
    <text/>
   </attribute>
   <element name="title">
    <text/>
   </element>
   <element name="author">
    <text/>
   </element>
   <zeroOrMore>
    <element name="character">
     <element name="name">
      <text/>
     </element>
     <optional>
      <element name="friend-of">
       <text/>
      </element>
     </optional>
     <element name="since">
      <text/>
     </element>
     <element name="qualification">
      <text/>
     </element>
    </element>
   </zeroOrMore>
  </element>
 </start>
</grammar>

Note that the rules to default the value of the ns attribute follow the same rules used to default the default namespace in XML: the default namespace doesn't apply to attributes, and this schema expects that the isbn attribute will be unqualified. If we want to specify that it will be qualified, we need to add the ns attribute in the attribute definition and write:

 <attribute name="isbn" ns="http://example.org/book">
  <text/>
 </attribute>

Importing definitions from external namespaces

Qualified names (qnames) can also be used for elements and attributes; since no namespace is attached to a RELAX NG schema, patterns cannot be referenced or defined using qualified names like W3C XML Schema types or groups. When using qnames, RELAX NG applies the same rule as XPath 1.0 and doesn't use the default namespace (defined in the RELAX NG schema considered as a XML document). The usage of qnames is aimed at making RELAX NG schemas less verbose when they mix several namespaces. For instance, to accept a xml:lang element in our title, we could write either

 <element name="title">
  <text/>
  <attribute name="lang" ns="http://www.w3.org/XML/1998/namespace">
   <value type="language"/>
  </attribute>
 </element>

or

 <element name="title" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <text/>
   <attribute name="xml:lang">
    <value type="language"/>
   </attribute>
 </element>

We could import the definition of the xml:lang attribute from another schema using the include element as we've seen earlier, but this is not mandatory.

Including unknown elements

As with W3C XML Schema, the inclusion of unknown elements is done through wildcards, but unlike W3C XML Schema, these wildcards are wildcards on the element and attribute names and not on the elements and attributes themselves. These wildcards (called "name classes") accept an except element, something missing from W3C XML Schema. The following shows the equivalent of the W3C complex type definition that allows any XHTML element within a mixed content element (the wildcards used here are "nsName", which means any name from a namespace and "anyName", which means any name from any namespace).

<define name="descType">
 <zeroOrMore>
  <choice>
   <text/>
   <element>
    <nsName ns="http://www.w3.org/1999/xhtml"/>
    <ref name="anyThing"/>
   </element>
  </choice>
 </zeroOrMore>
</define>

<define name="anyThing">
 <zeroOrMore>
  <choice>
   <text/>
   <attribute>
    <anyName/>
   </attribute>
   <element>
    <anyName/>
    <ref name="anyThing"/>
   </element>
  </choice>
 </zeroOrMore>
</define>

Note that RELAX NG doesn't include any predefined processing model (the equivalent of W3C XML Schema's "lax", "strict" or "skip") but since the wildcards are wildcards on the names only, you can and must define their content model and can choose to define what you like. Here we have defined a pattern named "anyThing", which we use as content model for the elements we accept from the XHTML namespace, which means that in W3C XML Schema terms we have a "skip" processing model.

Schema and Instance Documents

[ Same chapter for W3C XML Schema]

This section doesn't apply to RELAX NG which doesn't interfere with instance documents.

Concluding Comments

Throughout this comparison, we have seen that one of the main differences between the two languages is a matter of style: while RELAX NG focuses on generic "patterns", W3C XML Schema has differentiated these patterns into a set of distinct components (elements, attributes, groups, complex and simple types). The result is on one side a language which is lightweight and flexible (RELAX NG) and on the other side a language which gives more "meaning" or "semantic" to the components that it manipulates (W3C XML Schema). The question of whether the added features are worth the price in terms of complexity and rigidity is open, and the answer probably depends on the applications.

Independently of this first difference between the two, the different positions regarding "non-determinism" between RELAX NG, which accepts most of the constructs a designer can imagine, and W3C XML Schema, which is very strict, mean that a number of vocabularies which can be described by RELAX NG cannot be described by W3C XML Schema.

A way to summarize this is to notice that an implementation such as MSV (the "Multi Schema Validator" developed by Kohsuke Kawaguchi for Sun Microsystems) uses a RELAX NG internal representation as a basis to represent the grammar described in W3C XML Schema and DTD schemas. This seems to indicate that RELAX NG can be used as the base on which object oriented features such as those of W3C XML Schema can be implemented. The value of an XML-specific object-oriented layer is still to be determined, though, since generic object-oriented tools should be able to generate RELAX NG schemas directly.



1 to 3 of 3
  1. cosplay
    2010-07-27 23:43:01 cosplaywedding
  2. trying to convert XSD to RelaxNG
    2007-03-28 06:23:37 ndeb
  3. "br" element not well-formed?
    2002-10-10 09:09:37 Matt Plumtree
1 to 3 of 3