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"?>
<people>
</people>
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!):
<people/>
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">
<empty/>
</element>
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">
<text/>
</element>
And here's what the compact schema would look like:
element people { text }
Next: Adding child elements >>
More XML Articles
More By Peyton McCullough