RELAX NG by Eric van der Vlist will be published by O'Reilly & Associates (ISBN: 0596004214)

You are welcome to use our annotation system to give your feedback.


Complete schema

We now have all the patterns needed to write a full schema that would express all we've talked about for this example:

 <?xml version = '1.0' encoding = 'utf-8' ?>
 <element xmlns="http://relaxng.org/ns/structure/1.0" name="library">
  <oneOrMore>
   <element name="book">
    <attribute name="id"/>
    <attribute name="available"/>
    <element name="isbn">
     <text/>
    </element>
    <element name="title">
     <attribute name="xml:lang"/>
     <text/>
    </element>
    <oneOrMore>
     <element name="author">
      <attribute name="id"/>
      <element name="name">
       <text/>
      </element>
      <optional>
       <element name="born">
        <text/>
       </element>
      </optional>
      <optional>
       <element name="died">
        <text/>
       </element>
      </optional>
     </element>
    </oneOrMore>
    <zeroOrMore>
     <element name="character">
      <attribute name="id"/>
      <element name="name">
       <text/>
      </element>
      <optional>
       <element name="born">
        <text/>
       </element>
      </optional>
      <element name="qualification">
       <text/>
      </element>
     </element>
    </zeroOrMore>
   </element>
  </oneOrMore>
 </element>

RELAX NG directly supports four kinds of occurrence constraints on nodes: they may appear as exactly once (the default), optional, zero or more and one or more. These are the most common cases in document design. If applications need a finer level of control, they can achieve it by using or combining these four basic occurrence constraints. If, for instance, we needed to define that each book's description should have between two and six character elements, we could write the definition as two mandatory characters followed by four optional ones:

    <!-- 1 -->
    <element name="character">
     <attribute name="id"/>
     <element name="name">
      <text/>
     </element>
     <optional>
      <element name="born">
       <text/>
      </element>
     </optional>
     <element name="qualification">
      <text/>
     </element>
    </element>
    <!-- 2 -->
    <element name="character">
      .../...
    </element>
    <!-- 3 -->
    <optional>
     <element name="character">
     .../...
      </element>
     </element>
    </optional>
    <!-- 4 -->
    <optional>
     <element name="character">
     .../...
     </element>
    </optional>
    <!-- 5 -->
    <optional>
     <element name="character">
     .../...
     </element>
    </optional>
    <!-- 6 -->
    <optional>
     <element name="character">
     .../...
     </element>
    </optional>

This is certainly verbose, but in later chapters we will see how we can define and reuse patterns to reduce verbosity.

[Note]Note

W3C XML Schema offers much more control over how many times an element may appear, but this degree of control creates a number of processing complexities. While RELAX NG's approach may, in this respect, seem less powerful, it compensates by imposing far fewer costs.


You are welcome to use our annotation system to give your feedback.
[Annotations for this page]
All text is copyright Eric van der Vlist, Dyomedea. During development, I give permission for non-commercial copying for educational and review purposes. After publication, all text will be released under the Free Software Foundation GFDL.