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
|?||zero or one appearances, either appear all or nothing.|
|*||indicates any number of occurrences|
|+||one to any number of occurrences|
It is also possible to specify alternatives in a grammar, and group elements together:
||||one alternative out of the following set|
|()||a combination of elements, in the given order|
|+||allows child elements to occur in any order|
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.
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
type attribute specifies the name of the datatype in the library identified by the
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="http://www.w3.org/2001/XMLSchema-datatypes"/> </element>
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
For example, the following constrains the
<element name="email"> <data type="string"> <param name="maxLength">127</param> </data> </element>
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"> <choice> <value>html</value> <value>text</value> </choice> </attribute> </element>
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"> <list> <data type="float"/> <data type="float"/> </list> </element>
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