Chapter 7: RELAX NG


The previous two chapters looked at two of the more commonly used formats for defining XML vocabularies. This chapter continues this survey of schema languages by looking at RELAX NG. This is a relatively new XML schema definition language from the Organization for the Advancement of Structured Information Standards (OASIS). OASIS is a standards body-similar to the W3C-that was originally created as an improved version of DTDs, and can leverage some of the functionality of XML Schema. To further these goals, RELAX NG was designed to provide a highly readable format that could be used to define complex vocabularies.

This chapter will look at the two forms of the RELAX NG specification: the standard form and the compact form. It will show the way you use these two forms to define XML vocabularies, and compare it with DTDs and XML Schemas. Along the way, you will see how RELAX NG integrates with XML tools and programming languages for validating XML documents and creating objects that map to RELAX NG schemas.

Why Another Schema Language?

At first glance, there seems to be little need for yet another schema language. DTDs have been around since SGML, so most people are aware of them, and most applications can work with them. XML Schema is also commonly available, integrated with many XML tools, and ratified by the W3C. Still, there are great reasons why you should consider using RELAX NG.

The primary reason to use RELAX NG is simplicity. Anyone who has read the W3C XML Schema specification (all three parts) knows that they are not simple. RELAX NG was designed from the outset to be simple to understand and implement. Listing 7-1 shows a RELAX NG schema. Even without knowing the structure of a RELAX NG schema or reading the specification, it is fairly easy to understand the intent of each of the elements.

Listing 7-1: RELAX NG Schema

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <grammar        xmlns="http://relaxng.org/ns/structure/1.0"        datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">          <start>              <element name="productCatalog">                  <oneOrMore>                      <element name="product">                          <ref name="productDefinition"></ref>                      </element>                  </oneOrMore>              </element>          </start>          <define name="productDefinition">              <attribute name="id"><data type="integer"/></attribute>              <element name="shortName"><text /></element>              <element name="fullName"><text /></element>              <element name="description"><text /></element>              <element name="components">                  <ref name="productDefinition" />              </element>          </define>      </grammar> 
image from book

In the preceding schema, you see that it defines a grammar (the root node). Within that grammar, the root element is a productCatalog. This productCatalog includes one or more products. Each product, in turn, includes a number of sub-elements. The name of each pattern in a RELAX NG schema almost literally guides the reader to describe the structure, and this is one of the main benefits in its use.

In addition to simplicity, RELAX NG was defined to be much more modular and composable than DTDs or XML Schema. Features such as W3C Schema data types can be integrated with RELAX NG schemas. In addition, RELAX NG makes it easy to break your schema into multiple files. This enables you to define standardized schema elements (such as a standard address schema) and combine them into a larger schema.

Finally, RELAX NG provides two forms: a normal syntax that uses XML, and a compact syntax. This enables you to define your XML vocabularies using either the well-formed normal syntax, or the more DTD-like compact syntax.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net