Defining a RELAX NG Schema


The first step in using RELAX NG, as with other schema languages, is to sketch out your model. Identify the key elements in the XML syntax you are creating, determine the cardinality of each item, and decide if each item should be an element or attribute. When you are moderately comfortable with your decisions, you can begin to document them using RELAX NG syntax.

Defining a RELAX NG syntax is similar to creating one using DTD or W3C XML Schema syntax-the task that the three languages use is the same. Each attempts to describe the rules of a document, so that humans and/or software can know that the document successfully follows the rules. The actual differences between the three can be seen to be minor or major, depending on your perspective.

Listing 7-2 shows a simple DTD, Listing 7-3 shows the equivalent W3C XML Schema version, and 7-4 shows the RELAX NG schema.

Listing 7-2: XML document with an embedded DTD

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <!ELEMENT order (name,address,city,state,country,orderItems)*>      <!ELEMENT name (#PCDATA)>      <!ELEMENT address (#PCDATA)>      <!ELEMENT city (#PCDATA)>      <!ELEMENT state (#PCDATA)>      <!ELEMENT country (#PCDATA)>      <!ELEMENT orderItems (item,quantity)+>      <!ELEMENT item (#PCDATA)>      <!ELEMENT quantity (#PCDATA)> 
image from book

Listing 7-3: W3C XML Schema

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"            elementFormDefault="qualified">        <xs:element name="order">          <xs:complexType>            <xs:sequence minOccurs="0" maxOccurs="unbounded">              <xs:element name="name"  type="xs:string"/>              <xs:element name="address"  type="xs:string"/>              <xs:element name="city"  type="xs:string"/>              <xs:element name="state" type="xs:string"/>              <xs:element name="country" type="xs:string"/>              <xs:element ref="orderItems"/>            </xs:sequence>          </xs:complexType>        </xs:element>        <xs:element name="orderItems">          <xs:complexType>            <xs:sequence maxOccurs="unbounded">              <xs:element name="item"  type="xs:string"/>              <xs:element name="quantity" type="xs:int"/>            </xs:sequence>          </xs:complexType>        </xs:element>      </xs:schema> 
image from book

Listing 7-4: Simple RELAX NG Schema

image from book
      <?xml version="1.0" encoding="UTF-8"?>        <element name="order" xmlns="http://relaxng.org/ns/structure/1.0">          <zeroOrMore>            <element name="name"><text /></element>            <element name="address"><text /></element>                  <element name="city"><text /></element>            <element name="state"><text /></element>            <element name="country"><text /></element>            <element name="orderItems">              <oneOrMore>                <element name="item"><text /></element>                <element name="quantity"><text /></element>              </oneOrMore>            </element>          </zeroOrMore>        </element> 
image from book

As you can see from these examples, the main differences among the three schema syntaxes are how they use XML and identify cardinality. There are a few other differences, and the rest of this chapter will outline how you can write RELAX NG to define an XML schema.




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