Datatypes and More in RELAX NG - Using the grammar element
(Page 2 of 4 )
You may remember from the very first article that I said a RELAX NG implementation will actually transform a schema a bit, and the element element will no longer be the root element. The element that replaces it as the root element is the grammar element. Within this element is a single start element, which houses the main pattern.
We can easily modify the XML schema to use the grammar element. The existing schema simply needs to be put inside of a start element, which then needs to be put inside of a grammar element. After doing this, the entire XML schema would look like this:
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start>
<element name="people" datatypeLibrary=
"http://www.w3.org/2001/XMLSchema-datatypes">
<zeroOrMore>
<element name="person">
<optional>
<attribute name="date">
<data type="date"/>
</attribute>
</optional>
<interleave>
<choice>
<element name="zipCode">
<text/>
</element>
<group>
<element name="city">
<text/>
</element>
<element name="state">
<text/>
</element>
</group>
</choice>
<element name="gender">
<choice>
<value type="string">male</value>
<value type="string">female</value>
</choice>
</element>
</interleave>
</element>
</zeroOrMore>
</element>
</start>
</grammar>
Notice how the namespace declaration has been moved to the grammar element.
Using the compact syntax, the process is slightly different. In the compact syntax, there is a grammar keyword. After it are placed brackets, and the word start followed by an equal sign is placed within the brackets. The main pattern is then “assigned” to start. The resulting compact schema looks like this:
grammar {
start =
element people {
element person {
attribute date { xsd:date }?,
(
(element zipCode { xsd:string }
| (element city { text },
element state { text })) &
element gender { "male" | "female" }
)
}*
}
}
An instance document will still validate exactly as before.
Next: Creating named patterns >>
More XML Articles
More By Peyton McCullough