Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

RELAX NG

The most promising alternative to XML Schema is the RELAX NG standard, which is being sponsored by OASIS. The RELAX NG standard is the result of a positive reception among developers to a couple of proposed schema alternatives called TREX and RELAX, both of which were developed by individuals (James Clark and Murata Makoto, respectively) who were interested in offering a simpler alternative to XML Schemas.

RELAX NG combines the features of TREX and RELAX into one syntax, and it is XML-based. The goal of RELAX NG is to provide a simple, easy-to-learn syntax for describing grammars that is not necessarily hampered by backward compatibility and complexity (as is the case with XML Schemas).

Elements are declared in RELAX NG using the element element, which accepts a name attribute to specify the element name. To denote that an element has text content, you can use the text element. For example, the following declares an element called title that can contain a string:

<element name="title">
 <text/>
</element>

Other elements can be used as children of the element element to denote the occurrence of children, such as choice, optional, zeroOrMore, and oneOrMore.

These elements control content depending on the nesting order. For example, denote that an element is optional:

<element name="phone">
 <optional>
 <element name="extension">
  <text/>
 </element>
 </optional
</element>

This creates a phone element with an optional child element of extension.

The choice element allows us to create an "or" similar to the vertical bar ( | ) in a DTD:

<element name="preferred_number">
 <optional>
 <element name="phone">
  <text/>
 </element>
 <element name="cell">
  <text/>
 </element>
 </optional
</element>

This creates an element called preferred_number with a choice between phone and cell as the child element. Finally, to achieve the same results as the asterisk (*) and plus (+) symbols in a DTD, we have the zeroOrMore and the oneOrMore elements. These can be used to specify that a child element must occur either zero or more times, or one or more times, respectively:

<element name="name">
 <oneOrMore>
 <element name="phone">
  <text/>
 </element>
 </oneOrMore>
 <zeroOrMore>
 <element name="email">
  <text/>
 </element>
 </zeroOrMore>
</element>

For example, the RELAX NG element declaration above specifies that a name element must have at least one phone child, but could have more, and that the email element is optional, but may also contain multiple emails.

Attributes in RELAX NG are handled similarly to elements, and there is an attribute element that has a name attribute to specify the attribute's name:

<element name="phone">
 <text/>
 <attribute name="ext">
 <text/>
 </attribute>
</element>

The attribute declaration itself is nested within the element declaration for which the attribute is being defined. The attribute content is specified in the same manner as elements, with the text element being used for text.

In fact, you can also use the choice and optional elements with attributes:

<element name="phone">
 <text/>
 <optional>
 <attribute name="extension">
  <text/>
 </attribute>
 </optional>
</element>

This creates a phone element with an optional extension attribute.

Datatype support in RELAX NG is fairly robust. RELAX NG uses datatype libraries that allow you to specify the namespace for your datatypes, giving you access to any other datatype definition set, such as the XML Schema Recommendation, Part 2.

The datatype library can be specified for an individual element or attribute:

<element name="quantity">
<data type="integer" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"/>
</element>

Or it can be declared globally for your RELAX NG grammar:

<grammar xmlns="http://relaxng.org/ns/structure/0.9"
     ns="http://relaxng.org/ns/structure/0.9"
     datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">

To express constraining facets for datatypes, you can make use of the param element, which has a name attribute that can be used to express the name of the constraining facet, with the value of the facet contained in the param content:

<element name="quantity">
 <data type="integer" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
 <param name="minInclusive">0</param>
 <param name="maxInclusive">12</param>
 </data>
</element>

This allows you to exploit the full range of datatypes from other standards and incorporate them into your RELAX NG documents. Datatypes can be used with both elements and attributes in RELAX NG.

  • + Share This
  • 🔖 Save To Your Account