To start off, you will walk through a few simple examples of generating schemas with
Relaxer. Example documents mentioned in this tutorial are available in a ZIP
archive at http://www.relaxer.org/doc/tutorial/tutorial.zip.
Now consider the following XML document,
date.xml
:
<?xml version="1.0"?> <instant> <date type="ISO8601"> <year>2003</year> <month>01</month> <day>01</day> </date> </instant>
This simple, well-formed XML document uses a hierarchy of elements to represent an ISO 8601 date.
You can create a document type definition or DTD for date.xml
by issuing the following command:
C:\Relaxer>relaxer -verbose -dtd date.xml
The -verbose
switch (which is not required) allows you to see some
detail about Relaxer's output:
Copyright(c) 2000-2003 ASAMI,Tomoharu. All rights reserved. Relaxer Version 1.0RCb (20030115) by asami@relaxer.org Source file : file:/C:/Relaxer/date.xml artifact = date.dtd
The command produces the file date.dtd
, seen here:
<!-- Generated by Relaxer 1.0RCb --> <!-- Fri Jan 17 10:14:16 PST 2003 --> <!ELEMENT date (year, month, day)> <!ATTLIST date type CDATA #REQUIRED> <!ELEMENT year (#PCDATA)> <!ELEMENT day (#PCDATA)> <!ELEMENT instant (date)> <!ELEMENT month (#PCDATA)>
This result represents only one possible DTD for date.xml
. Relaxer
guesses at a feasible structure for a valid XML document based on the content of
the document date.xml
. For example, Relaxer supposes that the
type
attribute on the <date>
element is required.
If there were multiple instances of the <date>
element, and
type
occurred on only one of those instances, Relaxer would guess
that the attribute was optional (#IMPLIED
).
You can also generate a DTD based on two or more instances. Relaxer examines
the instances and decides on a best-fit DTD for them. Here is another document
that is similar to date.xml
but has a different content model. It
is called time.xml
:
<?xml version="1.0" encoding="UTF-8"?> <instant> <time> <hour>11</hour> <minute>33</minute> <second>14</second> <utc zone="GMT"/> </time> </instant>
If you want Relaxer to consider the content models of both documents, you can type this command:
C:\Relaxer>relaxer -verbose -dtd time.xml date.xml
This creates the DTD time.dtd
:
<!-- Generated by Relaxer 1.0RCb --> <!-- Fri Jan 17 10:18:05 PST 2003 --> <!ELEMENT date (year, month, day)> <!ATTLIST date type CDATA #REQUIRED> <!ELEMENT year (#PCDATA)> <!ELEMENT time (hour, minute, second, utc)> <!ELEMENT day (#PCDATA)> <!ELEMENT utc EMPTY> <!ATTLIST utc zone CDATA #REQUIRED> <!ELEMENT second (#PCDATA)> <!ELEMENT minute (#PCDATA)> <!ELEMENT hour (#PCDATA)> <!ELEMENT instant (date | time)> <!ELEMENT month (#PCDATA)>
Notice how the content model for <instant>
allows for a choice
of either a <date>
or a <time>
child.
In addition to DTDs, it is possible to generate other schemas with Relaxer, such as Relax Core, RELAX NG, and XML Schema.
To create a Relax Core schema, use the following command (note the -rxm
switch):
C:\Relaxer>relaxer -verbose -rxm date.xml
This produces the file date.rxm
shown here:
<?xml version="1.0" encoding="UTF-8" ?> <module xmlns="http://www.xml.gr.jp/xmlns/relaxCore" relaxCoreVersion="1.0" \ targetNamespace=""> <interface> <export label="instant"/> </interface> <elementRule label="instant"> <tag name="instant"/> <sequence> <ref label="date"/> </sequence> </elementRule> <elementRule label="date"> <tag name="date"> <attribute name="type" type="token"/> </tag> <sequence> <element name="year" type="int"/> <element name="month" type="int"/> <element name="day" type="int"/> </sequence> </elementRule> </module>
To generate a schema for both time.xml
and date.xml
, type
the following command line:
C:\Relaxer>relaxer -verbose -rxm time.xml date.xml
The artifact produced by this command is time.rxm
:
<?xml version="1.0" encoding="UTF-8" ?> <module xmlns="http://www.xml.gr.jp/xmlns/relaxCore" relaxCoreVersion="1.0" \ targetNamespace=""> <interface> <export label="instant"/> </interface> <elementRule label="instant"> <tag name="instant"/> <sequence> <choice> <ref label="time"/> <ref label="date"/> </choice> </sequence> </elementRule> <elementRule label="time"> <tag name="time"/> <sequence> <element name="hour" type="int"/> <element name="minute" type="int"/> <element name="second" type="int"/> <ref label="utc"/> </sequence> </elementRule> <elementRule label="utc"> <tag name="utc"> <attribute name="zone" type="token"/> </tag> <empty/> </elementRule> <elementRule label="date"> <tag name="date"> <attribute name="type" type="token"/> </tag> <sequence> <element name="year" type="int"/> <element name="month" type="int"/> <element name="day" type="int"/> </sequence> </elementRule> </module>
Now you'll generate schemas for RELAX NG.
To create a RELAX NG schema, try the following (note the -rng
switch):
C:\Relaxer>relaxer -verbose -rng date.xml
The result is the file date.rng
shown here:
<?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="instant"/> </start> <define name="instant"> <element name="instant"> <ref name="date"/> </element> </define> <define name="date"> <element name="date"> <attribute name="type"> <data type="token"/> </attribute> <element name="year"> <data type="int"/> </element> <element name="month"> <data type="int"/> </element> <element name="day"> <data type="int"/> </element> </element> </define> </grammar>
To generate a RELAX NG schema for both time.xml
and
date.xml
, use this command:
C:\Relaxer>relaxer -verbose -rng time.xml date.xml
The file produced by this command is time.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="instant"/> </start> <define name="instant"> <element name="instant"> <choice> <ref name="time"/> <ref name="date"/> </choice> </element> </define> <define name="time"> <element name="time"> <element name="hour"> <data type="int"/> </element> <element name="minute"> <data type="int"/> </element> <element name="second"> <data type="int"/> </element> <ref name="utc"/> </element> </define> <define name="utc"> <element name="utc"> <attribute name="zone"> <data type="token"/> </attribute> </element> </define> <define name="date"> <element name="date"> <attribute name="type"> <data type="token"/> </attribute> <element name="year"> <data type="int"/> </element> <element name="month"> <data type="int"/> </element> <element name="day"> <data type="int"/> </element> </element> </define> </grammar>
Finally, you will create schemas in the W3C XML Schema Description Language (XSD).
For XML Schema, use the -xsd
switch:
C:\Relaxer>relaxer -verbose -xsd date.xml
The file this command produces is date.xsd
, shown here:
<?xml version="1.0" encoding="UTF-8" ?> <xsd:schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="instant" type="instant"/> <xsd:complexType name="instant"> <xsd:sequence> <xsd:element name="date" type="date"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="date"> <xsd:sequence> <xsd:element name="year" type="xsd:int"/> <xsd:element name="month" type="xsd:int"/> <xsd:element name="day" type="xsd:int"/> </xsd:sequence> <xsd:attribute name="type" type="xsd:token"/> </xsd:complexType> </xsd:schema>
To generate an XSD schema for both time.xml
and date.xml
,
do the following:
C:\Relaxer>relaxer -verbose -xsd time.xml date.xml
This creates time.xsd
:
<?xml version="1.0" encoding="UTF-8" ?> <xsd:schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="instant" type="instant"/> <xsd:complexType name="instant"> <xsd:sequence> <xsd:choice> <xsd:element name="time" type="time"/> <xsd:element name="date" type="date"/> </xsd:choice> </xsd:sequence> </xsd:complexType> <xsd:complexType name="time"> <xsd:sequence> <xsd:element name="hour" type="xsd:int"/> <xsd:element name="minute" type="xsd:int"/> <xsd:element name="second" type="xsd:int"/> <xsd:element name="utc" type="utc"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="utc"> <xsd:sequence/> <xsd:attribute name="zone" type="xsd:token"/> </xsd:complexType> <xsd:complexType name="date"> <xsd:sequence> <xsd:element name="year" type="xsd:int"/> <xsd:element name="month" type="xsd:int"/> <xsd:element name="day" type="xsd:int"/> </xsd:sequence> <xsd:attribute name="type" type="xsd:token"/> </xsd:complexType> </xsd:schema>
Next, you will learn how to generate Java code with Relaxer.