XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

An Introduction to the Relaxer Schema Compiler

February 19, 2003

Relaxer is a Java schema compiler for the XML schema languages RELAX NG and Relax Core. Using the Document Object Model, among other APIs, Relaxer generates Java classes based on schemas. It can also create classes based on XML document instances. The classes that Relaxer generates provide methods that allow you to access instances that are valid with regard to the compiled schemas, for use in your own programs that rely on the generated classes.

In addition to compiling schemas, Relaxer can also generate DTDs, RELAX NG schemas, Relax Core schemas, W3C XML Schema (WXS) schemas, and XSLT stylesheets. You can also create Java classes that support, among other things, SAX, Java Database Connectivity (JDBC), classic design patterns, such as composite and visitor, factories, and components for Enterprise JavaBeans, Remote Method Invocation (RMI), and the Java API for XML Messaging (JAXM). The generation of schemas, stylesheets, and Java classes will be covered in this article.

We will present a series of brief examples. In order to run these, you will need the SDK for Java 2 version 1.4 or higher (the latest SDKs are available for download from Sun's Java web site). Relaxer requires JAXP (Java API for XML Processing), which comes with version 1.4. You can run Relaxer with earlier versions of Java, such as 1.3.1, but it requires a separate download and installation of JAXP.

You will also need a copy of the latest version of Relaxer, available for download. The example documents discussed in this article are likewise available for download, and have all been tested on a Microsoft Windows XP Professional platform, running Java 2 version 1.4.1_01, and using a release candidate for Relaxer 1.0 (1.0RCb).

Installing Relaxer

Relaxer is easy to install. After you download the latest program archive file from the Relaxer site (called either setup.zip or beta.zip), and save it to a working directory, run the Relaxer setup utility program by typing the following command at a shell or command prompt:

java -jar setup.zip

The utility walks you through a few installation steps, such as choosing the directory where you want Relaxer installed. When you're done, you'll notice a couple files in the installation directory, one a shell script named relaxer and another a batch file named relaxer.bat. The scripts help to keep Relaxer easy to run, because it uses an absolute path to Relaxer.jar, Relaxer's Java archive

The install program determines the classpath for Relaxer.jar from the installation process, and adds the classpath to the script and batch files. Place the Relaxer installation directory in your PATH variable, and you'll be ready to try out the examples. As long as the Relaxer installation directory is in your path, you won't have to set the classpath separately to run the examples if you use the script or batch file.

Generating Schemas

First off, here is a simple XML document that you can run through Relaxer's paces. You'll find this file, as well as all other files mentioned in this article, in the downloadable example archive. Assuming that you are working in the directory where you unzipped the examples, you should see a file called album01.xml:

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

<album id="HANC-20241">
 <title>The Best of Patsy Cline</title>
 <manufacturer>MCA Records, Inc.</manufacturer>
 <release>1985</release>
 <format>cassette</format>
 <condition>good</condition>
</album>

Using the Relaxer script or batch file, you can generate a DTD with the -dtd option in this way:


relaxer -dtd album01.xml

You can use the optional -verbose option if you want to see a report on Relaxer's activities. This command with the -dtd option produce a single artifact, a file named album01.dtd in the current directory:

<!-- Generated by Relaxer 1.0RCb -->
<!-- Tue Feb 04 13:45:40 PST 2003 -->

<!ELEMENT manufacturer (#PCDATA)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT release (#PCDATA)>

<!ELEMENT album (title, manufacturer, release, format, condition)>
<!ATTLIST album id CDATA #REQUIRED>

<!ELEMENT artist (#PCDATA)>

<!ELEMENT condition (#PCDATA)>

<!ELEMENT format (#PCDATA)>

Relaxer constructs only a feasible content model in the DTD, based on some logical guesswork. For example, Relaxer assumes that the id attribute on <album> is a required attribute (#REQUIRED), and that each child of <album> must occur exactly once. Relaxer can take more than one instance as input, however, and as a result, can make more accurate guesses as to what the content model should be.

The following document, album02.xml, is similar to album01.xml, but it does not have an id attribute on the <album> element. It also tacks a <comments> element on after <condition>:

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

<album>
 <title>The Best of the Sons of the Pioneers</title>
 <artist>The Sons of the Pioneers</artist>
 <manufacturer>RCA</manufacturer>
 <release>no date</release>
 <format>cassette</format>
 <condition>fair</condition>
 <comments>hold for Hank Vale</comments>
</album>

Submit both album01.xml and album02.xml on the command line with Relaxer, like this:

relaxer -dtd album02.xml album01.xml

This command produces a different DTD, one that reflects and balances the content of both documents. Relaxer automatically assumes the name of the first argument as the file name for the DTD (album02.dtd):

<!-- Generated by Relaxer 1.0RCb -->
<!-- Tue Feb 04 16:32:37 PST 2003 -->

<!ELEMENT comments (#PCDATA)>

<!ELEMENT manufacturer (#PCDATA)>

<!ELEMENT title (#PCDATA)>

<!ELEMENT release (#PCDATA)>

<!ELEMENT album (title, artist, manufacturer, release, format, condition, comments?)>
<!ATTLIST album id CDATA #IMPLIED>

<!ELEMENT artist (#PCDATA)>

<!ELEMENT condition (#PCDATA)>

<!ELEMENT format (#PCDATA)>

Relaxer now assumes that the id attribute is optional (#IMPLIED) and declares a <comments> element, allowing one or zero occurrences (?) of <comments> in the content model for <album>.

Similar command-line options allow you to build other kinds of schemas. For example, to create a RELAX NG schema from these instances, use the -rng switch:

relaxer -rng album01.xml album02.xml

This generates the RELAX NG schema album01.rng:

<?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>
    <ref name="album"/>
  </start>
  <define name="album">
    <element name="album">
      <optional>
        <attribute name="id">
          <data type="token"/>
        </attribute>
      </optional>
      <element name="title">
        <data type="token"/>
      </element>
      <element name="artist">
        <data type="token"/>
      </element>
      <element name="manufacturer">
        <data type="token"/>
      </element>
      <element name="release">
        <data type="int"/>
      </element>
      <element name="format">
        <data type="token"/>
      </element>
      <element name="condition">
        <data type="token"/>
      </element>
      <optional>
        <element name="comments">
          <data type="token"/>
        </element>
      </optional>
    </element>
  </define>
</grammar>

Relaxer chooses the token type from XML Schema datatypes (XSD) for elements that could just as easily be string types (<artist>, for example). This is because the token type in XSD is a tokenized string that accepts single spaces between tokens.

Nonetheless, album01.rng is simply Relaxer's attempt to write a feasible schema. You are welcome, if not expected, to adjust this schema to suit your needs and preferences.

The next command creates a Relax Core schema, album01.rxm:

relaxer -rxm album01.xml album02.xml

Lastly, the following command creates the WXS schema album01.xsd by using the -xsd option:

relaxer -xsd album01.xml album02.xml

We won't show these last two schemas to you here, but you can look at them yourself with a text editor (they are in with the downloaded archive files). Creating XSLT stylesheets with Relaxer is just as easy as generating schemas.

Pages: 1, 2

Next Pagearrow