Very diverse use cases:
import libxml2
reader = libxml2.newTextReaderFilename("ex.xml")
ret = reader.Read()
while ret == 1:
print reader.Name()
ret = reader.Read()
More extensive validation than DTDs
Counter proposal to W3C XML Schemas
Consider some simple XML:
<addressBook>
<card>
<name>John Smith</name>
<email>js@example.com</email>
</card>
<card>
<name>Fred Bloggs</name>
<email>fb@example.net</email>
</card>
</addressBook>
The DTD for it:
<!DOCTYPE addressBook [ <!ELEMENT addressBook (card*)> <!ELEMENT card (name, email)> <!ELEMENT name (#PCDATA)> <!ELEMENT email (#PCDATA)> ]>
The Relax-NG schema for it:
<element name="addressBook" xmlns="http://relaxng.org/ns/structure/1.0">
<zeroOrMore>
<element name="card">
<element name="name">
<text/>
</element>
<element name="email">
<text/>
</element>
</element>
</zeroOrMore>
</element>
If using attributes:
<element name="card">
<attribute name="name">
<text/>
</attribute>
<attribute name="email">
<text/>
</attribute>
</element>
Relax-NG has an unified model for attributes and elements.
Using attributes or elements:
<element name="card">
<choice>
<element name="name">
<text/>
</element>
<attribute name="name">
<text/>
</attribute>
</choice>
<choice>
<element name="email">
<text/>
</element>
<attribute name="email">
<text/>
</attribute>
</choice>
</element>
Relax-NG allows to type-check textual content
Reuse of the W3C XML Schemas Datatype library
Modular structure using includes
Use the define and ref to reuse blocks
<element name="card">
<ref name="cardContent"/>
</element>
...
<define name="cardContent">
<element name="name">
<text/>
</element>
<element name="email">
<text/>
</element>
</define>