XML - RELAX NG Schema Language
Introduction
RELAX NG (Regular Language for XML Next Generation) is a schema language used to define the structure and rules of XML documents. Like XML Schema (XSD) and DTD (Document Type Definition), RELAX NG ensures that XML documents follow a predefined format. However, RELAX NG is widely appreciated for its simplicity, flexibility, and ease of writing. It allows developers to create clear and maintainable validation rules without the complexity often associated with XML Schema.
RELAX NG was standardized by the International Organization for Standardization (ISO/IEC 19757-2) and is used in several XML-based technologies, including document publishing, technical documentation, office document formats, and data exchange systems.
Why RELAX NG Was Developed
Before RELAX NG, developers primarily used DTDs and XML Schema for validation. While DTDs were simple, they lacked support for namespaces and data types. XML Schema introduced powerful features but became complicated due to its extensive syntax and numerous elements.
RELAX NG was designed to offer:
-
A simpler syntax
-
Better readability
-
Flexible validation rules
-
Easy maintenance
-
Strong namespace support
-
Compatibility with existing XML standards
Its goal is to make schema development easier without sacrificing validation capabilities.
Two Syntax Formats
RELAX NG provides two different ways to write schemas.
XML Syntax
The XML syntax is written entirely in XML format. Since it is XML itself, it can be processed using standard XML tools.
Example:
<element name="student"
xmlns="http://relaxng.org/ns/structure/1.0">
<element name="name">
<text/>
</element>
<element name="age">
<data type="integer"
xmlns="http://www.w3.org/2001/XMLSchema-datatypes"/>
</element>
</element>
Although this syntax is machine-friendly, it can become lengthy.
Compact Syntax
RELAX NG Compact Syntax (RNC) provides a shorter and more readable format.
Example:
student = element student {
element name { text },
element age { xsd:integer }
}
Many developers prefer compact syntax because it is easier to write and understand.
Basic Structure of RELAX NG
A schema defines the allowed structure of an XML document.
Suppose the XML document is:
<book>
<title>XML Fundamentals</title>
<author>John Smith</author>
</book>
The corresponding RELAX NG Compact Schema would be:
book = element book {
element title { text },
element author { text }
}
This schema specifies that:
-
The root element must be
<book> -
It must contain a
<title> -
It must contain an
<author> -
Both elements contain text
Defining Elements
Elements are defined using the element keyword.
Example:
employee = element employee {
element id { text },
element name { text }
}
Valid XML:
<employee>
<id>1001</id>
<name>Rahul</name>
</employee>
Invalid XML:
<employee>
<id>1001</id>
</employee>
The <name> element is missing, so validation fails.
Defining Attributes
RELAX NG allows attributes to be defined inside elements.
Example:
employee = element employee {
attribute department { text },
element name { text }
}
Valid XML:
<employee department="HR">
<name>Priya</name>
</employee>
The schema requires the department attribute.
Data Types
RELAX NG supports XML Schema data types.
Example:
student = element student {
element name { text },
element marks { xsd:decimal }
}
Valid XML:
<student>
<name>Arun</name>
<marks>92.5</marks>
</student>
Invalid XML:
<student>
<name>Arun</name>
<marks>Ninety Two</marks>
</student>
Since "Ninety Two" is not a decimal value, validation fails.
Common supported data types include:
-
string
-
integer
-
decimal
-
boolean
-
date
-
dateTime
-
float
-
double
Optional Elements
Optional elements are marked with a question mark (?).
Example:
employee = element employee {
element name { text },
element phone { text }?
}
The phone element may appear once or not appear at all.
Repeated Elements
An asterisk (*) allows zero or more occurrences.
Example:
library = element library {
element book { text }*
}
Possible XML:
<library>
</library>
or
<library>
<book>XML</book>
<book>Java</book>
<book>Python</book>
</library>
Both are valid.
A plus sign (+) means one or more occurrences.
Example:
library = element library {
element book { text }+
}
At least one <book> must exist.
Choice Between Elements
The pipe symbol (|) represents alternatives.
Example:
payment = element payment {
element cash { text }
|
element card { text }
}
The XML document can contain either a cash element or a card element.
Grouping Elements
Multiple elements can be grouped together.
Example:
person = element person {
element firstname { text },
element lastname { text },
element age { xsd:integer }
}
All three elements must appear in the specified order.
Mixed Content
RELAX NG supports mixed content where text and elements appear together.
Example:
paragraph = element p {
mixed {
element bold { text }*
}
}
Valid XML:
<p>
Learning
<bold>XML</bold>
is useful.
</p>
Text and child elements are allowed together.
Pattern Reuse
One of RELAX NG's strengths is defining reusable patterns.
Example:
address =
element address {
element city { text },
element country { text }
}
customer =
element customer {
element name { text },
address
}
supplier =
element supplier {
element company { text },
address
}
The same address pattern is reused by multiple elements, reducing duplication.
Namespace Support
RELAX NG handles XML namespaces naturally.
Example:
namespace x = "http://example.com/books"
book = element x:book {
element x:title { text }
}
Namespaces help distinguish elements that belong to different XML vocabularies.
Validation Process
The validation process generally follows these steps:
-
Create an XML document.
-
Write a RELAX NG schema.
-
Use a validator that supports RELAX NG.
-
Compare the XML document against the schema.
-
Report any validation errors.
-
Correct the XML if necessary.
-
Validate again until no errors remain.
Advantages of RELAX NG
-
Simple and readable syntax.
-
Easier to learn than XML Schema.
-
Excellent namespace support.
-
Supports reusable patterns.
-
Less verbose than XSD.
-
Supports XML Schema data types.
-
Flexible validation rules.
-
Easy to maintain for large projects.
-
Well suited for document-centric XML applications.
-
Available in both XML and compact syntax formats.
Limitations of RELAX NG
-
Less widely adopted than XML Schema.
-
Limited support in some XML development tools.
-
Fewer learning resources are available.
-
Some enterprise applications support only XSD.
-
Developers familiar with XML Schema may require time to adapt.
RELAX NG vs XML Schema (XSD)
| Feature | RELAX NG | XML Schema (XSD) |
|---|---|---|
| Syntax Complexity | Simple | More complex |
| Readability | High | Moderate |
| Learning Curve | Easier | Steeper |
| Namespace Support | Excellent | Excellent |
| Data Types | Uses XML Schema data types | Built-in |
| Reusable Patterns | Very flexible | Flexible |
| Tool Support | Moderate | Extensive |
| ISO Standard | Yes | W3C Recommendation |
| Compact Syntax | Yes | No |
| Enterprise Adoption | Moderate | Very High |
Common Applications
RELAX NG is commonly used in:
-
Digital publishing systems
-
Technical documentation
-
Office document standards
-
Scientific data exchange
-
XML configuration files
-
Government XML document standards
-
Digital libraries
-
Content management systems
-
Documentation frameworks
-
XML-based publishing workflows
Best Practices
-
Use compact syntax for better readability.
-
Organize large schemas into reusable patterns.
-
Choose meaningful names for elements and patterns.
-
Validate XML documents regularly during development.
-
Keep schemas modular for easier maintenance.
-
Document complex validation rules for future developers.
-
Use namespaces consistently when combining multiple XML vocabularies.
-
Test schemas with different XML documents to ensure reliability.
Conclusion
RELAX NG is a powerful and elegant schema language that simplifies XML validation while maintaining strong validation capabilities. Its clean syntax, reusable patterns, and flexibility make it an excellent alternative to XML Schema for many XML applications. Although it is not as widely adopted as XSD, RELAX NG remains a valuable choice for projects that prioritize readability, maintainability, and efficient schema design. Understanding RELAX NG gives developers another effective tool for creating reliable XML documents and building robust XML-based systems.