Home arrow XML arrow Page 2 - An Introduction to RELAX NG

An Introduction to RELAX NG

RELAX NG is not a capitalized misspelling of something you probably get to do all too rarely as a busy programmer and web designer. If you use XML to any great degree, you'll want to take a close look at it. It can help make your life as a web developer easier, allowing you to relax a little more. Keep reading to learn what it is, and the advantages to using it. This is the first part of a three-part series.

Author Info:
By: Peyton McCullough
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
July 28, 2008
  1. · An Introduction to RELAX NG
  2. · Getting started with elements
  3. · Adding child elements
  4. · Adding attributes

print this article

An Introduction to RELAX NG - Getting started with elements
(Page 2 of 4 )

Let's get started with RELAX NG. The most fundamental thing to do with any grammar-based schema language is to represent an element. So, we'll start by examining a very simple XML document.

Consider an XML document representing a group of people. This document might have a people element as the root element, which has child elements named person to store data about individual people. Only, for now, let's trim the document down so that only the root element is present:

<?xml version="1.0" encoding="UTF-8"?>

Creating a schema built around this document isn't very difficult at all because only a single element is present. The only thing we have to keep in mind is content—what does the people element contain? Normally, it would contain other elements, but since nothing is in it at the moment, we'll treat it as an empty element. So, although this parts from the example a bit, the following XML would be equivalent (but just for now!):


We'll start with a schema using RELAX NG's XML syntax. Elements are represented using the element element. An element can be given a name with the name attribute. In a schema, the element tag can actually serve as the root element. We don't have to wrap it in another special element, although the validator will do so behind the scenes if we don't. But I'm getting ahead of myself here, and we'll get to that part later.

Since we decided to treat the people element as an empty element, we must mark it so using the empty element, which must be a child of element. We must also add the appropriate namespace declaration, using “http://relaxng.org/ns/structure/1.0” as the URI. Here is the complete schema for the above XML, using RELAX NG's XML syntax:

<element name="people"  xmlns="http://relaxng.org/ns/structure/1.0">

That wasn't too bad at all. Looking at the schema, it's very easy to tell exactly what an instance document would look like.

Now let's take a look at the schema using the compact syntax. The compact syntax uses some of the same vocabulary as the XML syntax. Elements are declared using element, and the element is declared to be empty using empty. However, the result is remarkably concise. Here's what it looks like:

element people { empty }

Above, the element's name immediately follows element, and the element's children are contained in curly brackets. The schema built using the compact syntax is shorter than the first schema, yet the two produce the exact same result.

If we want the element to contain text rather than be empty, then the transformation isn't very difficult. We simply need to substitute empty with text. Here's what the XML schema would look like:

<element name="people"  xmlns="http://relaxng.org/ns/structure/1.0">

And here's what the compact schema would look like:

element people { text }

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials