Relax NG, Compared
by Eric van der Vlist
|
Pages: 1, 2, 3, 4, 5
Content Types
|
Table of Contents |
|
Introducing Our First Schema |
[ Corresponding chapter for W3C XML Schema]
All the content types are handled through patterns, and there is no difference between the ways one constructs simple, complex, mixed, or empty content models with RELAX NG.
To define empty content models, you just leave the content empty (i.e. omit any embedded text, value or element declaration):
<element name="book"> <attribute name="isbn"> <ref name="isbnType"/> </attribute> </element>
To define a simple content model, you just omit the declaration of any embedded element:
<element name="book"> <attribute name="isbn"> <ref name="isbnType"/> </attribute> <text/> </element>
To define a complex content model (not mixed), you just omit the declaration of any embedded text:
<element name="book"> <attribute name="isbn"> <text/> </attribute> <element name="title"> <text/> </element> <element name="author"> <text/> </element> </element>
To define mixed content models you declare both embedded text and elements:
<element name="book">
<attribute name="isbn">
<text/>
</attribute>
<interleave>
<element name="title">
<text/>
</element>
<element name="author">
<text/>
</element>
<zeroOrMore>
<text/>
</zeroOrMore>
</interleave>
</element>
|
|
| Post your comments |
Since text nodes are handled like elements and attributes, their individual location and type can be defined and constrained, something which isn't possible with W3C XML Schema. Suppose we have an element p containing lines terminated by empty br elements, and that we want to disallow blank lines. We can write
<element name="p"> <zeroOrMore> <text/> <element name="br"> </zeroOrMore> <optional> <text/> </optional> </element>
Constraints
[ Corresponding chapter for W3C XML Schema]
This chapter will be a short one. RELAX NG does not provide any feature to define keys and key references. A workaround can be to embed Schematron rules enforcing these constraints into your RELAX NG schema as supported by some implementations.
Building Usable -- and Reusable -- Schemas
[ Corresponding chapter for W3C XML Schema]
RELAX NG doesn't include anything similar to the xs:annotation W3C XML Schema element. Instead it provides much less or much more, depending on how you want to consider it. RELAX NG processors simply ignore any element or attribute with a namespace URI different from the RELAX NG namespace URI. So we can just help ourselves and use any element from any namespace we like to annotate RELAX NG Schemas. An example (similar to what we've seen with W3C XML Schema) could be
<element name="book">
<annotation xmlns="http://example.com/doc">
<documentation xml:lang="en">
Top level element.
</documentation>
<documentation xml:lang="fr">
Element racine.
</documentation>
<appinfo source="http://example.com/foo/">
<bind xmlns="http://example.com/bar/">
<class name="Book"/>
</bind>
</appinfo>
</annotation>
.../...
</element>
Composing schemas from multiple files
The RELAX NG include element provides the same kind of functionality as W3C XML Schema's xs:include and xs:redefine: you can use it without any child elements to perform a straight inclusion of the patterns defined in another RELAX NG schema or embed in the include element any pattern redefinition. The restriction of W3C XML Schema that any redefinition has to be a valid derivation of the base type doesn't exist in RELAX NG: the redefinition may be as different from the base pattern as you want it to be. Our W3C XML Schema would then become in RELAX NG:
<include href="foo.rng"> <define name="nameType"> <data type="token"> <param name="maxLength">40</param> </data> </define> </include>
Substitution groups and abstract elements
There are no substitution groups in RELAX NG. However, it provides
a way to define which rule may be applied when a pattern is redefined,
which can play a similar role through the combine
attribute. This attribute, belonging to the define element,
may take the values "choice" or "interleave". When its value is
"choice", a reference to the pattern may match either definition. For
example, a reference to the following definition will accept either a
name or a subname element.
<define name="name" combine="choice"> <element name="name"> <text/> </element> </define> <define name="name" combine="choice"> <element name="surname"> <text/> </element> </define>
Since the names of the definitions and references are naming RELAX NG patterns, and not elements or attributes, there is no need here to define abstract elements.
Final types
The notion of final type is closely bound to the W3C XML Schema derivation features and does not apply to RELAX NG, which is focused on defining reusable patterns.