any experienced developers loathe changing their DTDs (Document Type Definitions) because of their cumbersome syntax and semantics. For newcomers, DTD syntax just looks like Greek. A brand new addition to the DSDL (Document Schema Definition Languages) called Regular Language Description for XML (RELAX NG) intends to change that. The following are RELAX NG's main goals:
- Compact syntax: Maximize readability, simplify the syntax (RELAX NG schema is very close to a textual description of a vocabulary.)
- Compatibility: Backward compatibility starting from XML 1.0 DTD
In fact, since it is defined in XML, RELAX NG inherits the entire XML specification, including data types, infosets, and namespaces. While XSD (XML Schema Definition) was developed to define schemas in the XML format, RELAX NG supports pluggable simple data type libraries (new ones can be readily designed and built as needed) and provides two interconvertible syntaxes: an XML one for processing and a compact non-XML one for human authoring.
This article provides in-depth details of XML-based RELAX NG tags, providing a comparison to XSDs and DTDs wherever applicable. It provides an example that defines the schema in RELAX NG and validates it using JAXB from within a Java application.
RELAX NG Data Model
The RELAX NG abstract data model deals with XML documents that represent both schemas and instances. An XML document is represented by an element, which consists of a name, context, a set of attributes, and an ordered sequence of zero or more children. Consider the following RELAX NG example of student roll:
<element name="studentRoll" xmlns="http://relaxng.org/ns/structure/1.0">
RELAX NG uses self-explanatory numerical pattern quantifications, such as <zeroOrMore>, <oneOrMore>, and <optional> tags, which would be represented by '*', '+', and '?' respectively in a DTD:
<element name="firstName"> <text/></element>
<data type="integer" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"/>
The <choice> tag provides the functionality of OR ('|'). In the above snippet, the schema says that a studentID consists of either just a name or a first and last name.