Browse DevX
Sign up for e-mail newsletters from DevX


Discover RELAX NG, A Simple Schema Solution

With the goals of compact syntax and compatibility, the Regular Language Description for XML (RELAX NG) eases the pain of working with cumbersome DTDs and XSDs.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

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"> <zeroOrMore> <element name="studentID"> <element name="name"><text/></element> <element name="ssn"><text/></element> </element> </zeroOrMore> </element>

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="studentID"> <choice> <element name="name"><text/></element> <group> <element name="firstName"> <text/></element> <element name="lastName"><text/></element> </group> </choice> <attribute name="age"> <data type="integer" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"/> </attribute> <attribute name="sex"> <choice> <value>"Male"</value> <value>"Female"</value> </choice> </attribute> </element>

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.

Thanks for your registration, follow us on our social networks to keep up-to-date