Introduction to RELAX NG (1/2) - exploring XML | 2 | WebReference

Introduction to RELAX NG (1/2) - exploring XML | 2

Introduction to RELAX NG


While we have seen how elements and attributes are defined, we also need to learn how to specify cardinality. This is done through special elements, of which we have already seen the optional tag:

optional?zero or one appearances, either appear all or nothing.
zeroOrMore*indicates any number of occurrences
oneOrMore+one to any number of occurrences


It is also possible to specify alternatives in a grammar, and group elements together:

choice|one alternative out of the following set
group()a combination of elements, in the given order
interleave+allows child elements to occur in any order

Data typing

RELAX NG allows patterns to reference externally-defined datatypes, such as those defined by the W3C XML Schema Datatypes. RELAX NG implementations may differ in what datatypes they support. You must use datatypes that are supported by the implementation you plan to use.

The data pattern matches a string that represents a value of a named datatype. The datatypeLibrary attribute contains a URI identifying the library of datatypes being used. The datatype library defined by W3C XML Schema Datatypes would be identified by the URI The type attribute specifies the name of the datatype in the library identified by the datatypeLibrary attribute. For example, if a RELAX NG implementation supported the datatypes of W3C XML Schema Datatypes, you could use:

<element name="number">
  <data type="integer" datatypeLibrary=""/>

Datatypes may have parameters. For example, a string datatype may have a parameter controlling the length of the string. The parameters applicable to any particular datatype are determined by the datatyping vocabulary. Parameters are specified by adding one or more param elements as children of the data element. For example, the following constrains the email element to contain a string at most 127 characters long:

<element name="email">
  <data type="string">
    <param name="maxLength">127</param>

Complex types

RELAX NG also supports enumerations and lists outside of any supported data type library. Many markup vocabularies have attributes whose value is constrained to be one of a set of specified values. The value pattern matches a string that has a specified value. For example,

<element name="newsletter">
  <attribute name="preferredFormat">

allows the preferredFormat attribute to have the value html or text. This corresponds to the DTD:

<!DOCTYPE newsletter [
<!ELEMENT newsletter EMPTY>
<!ATTLIST newsletter
  preferredFormat (html|text) #REQUIRED>

The list pattern matches a whitespace-separated sequence of tokens; it contains a pattern that the sequence of individual tokens must match. The list pattern splits a string into a list of strings, and then matches the resulting list of strings against the pattern inside the list pattern.

For example, suppose we want to have a vector element that contains two floating point numbers separated by whitespace. We could use list as follows:

<element name="vector">
    <data type="float"/>
    <data type="float"/>


This concludes our quick tour of RELAX NG. Review the official specification for some of the less often used features. It will be interesting to see whether RELAX NG or XML Schema will enjoy more success as a replacement for DTDs.

Produced by Michael Claßen

Created: Jul 22, 2002
Revised: Jul 22, 2002