Exemple : Carnet d'adresse (XML, DTD, Relax NG)

ExempleInstance XML

1
<?xml version="1.0" encoding="UTF-8"?><?oxygen RNGSchema="adresse1.rnc" type="compact"?>
2
<addressBook>
3
    <card>
4
        <name>John Smith</name>
5
        <email>js@example.com</email>
6
    </card>
7
    <card>
8
        <name>Fred Bloggs</name>
9
        <email>fb@example.net</email>
10
    </card>
11
</addressBook>

ExempleDTD

1
<!ELEMENT addressBook (card*)>
2
<!ELEMENT card (name, email)>
3
<!ELEMENT name (#PCDATA)>
4
<!ELEMENT email (#PCDATA)>

ExempleRelaxNG (version 1 : sans pattern, XML)

1
<?xml version="1.0" encoding="UTF-8"?>
2
<element name="addressBook" xmlns="http://relaxng.org/ns/structure/1.0">
3
    <oneOrMore>
4
        <element name="card">
5
            <element name="name"><text/></element>
6
            <element name="email"><text/></element>
7
        </element>
8
    </oneOrMore>
9
</element>

ExempleRelaxNG (version 1 : sans pattern, compacte)

1
element addressBook {
2
  element card {
3
    element name { text },
4
    element email { text }
5
  }+
6
}

ExempleRelaxNG (version 2 : avec pattern, XML)

1
<?xml version="1.0" encoding="UTF-8"?>
2
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
3
    <start>
4
        <element name="addressBook">
5
            <zeroOrMore>
6
                <element name="card">
7
                    <ref name="cardContent"/>
8
                </element>
9
            </zeroOrMore>
10
        </element>
11
    </start>
12
    <define name="cardContent">
13
        <element name="name">
14
            <text/>
15
        </element>
16
        <element name="email">
17
            <text/>
18
        </element>
19
    </define>
20
</grammar>

ExempleRelaxNG (version 2 : avec pattern, compacte)

1
namespace rng = "http://relaxng.org/ns/structure/1.0"
2
start = AddressBook
3
AddressBook = element addressBook { Card* }
4
Card = element card { Name, Email }
5
Name = element name { text }
6
Email = element email { text }

ExempleRelaxNG (version 3 : avec datatype, XML)

1
<?xml version="1.0" encoding="UTF-8"?>
2
<grammar 
3
    xmlns="http://relaxng.org/ns/structure/1.0"
4
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
5
    <start>
6
        <element name="addressBook">
7
            <zeroOrMore>
8
                <element name="card">
9
                    <ref name="cardContent"/>
10
                </element>
11
            </zeroOrMore>
12
        </element>
13
    </start>
14
    <define name="cardContent">
15
        <element name="name">
16
            <data type="string"/>
17
        </element>
18
        <element name="email">
19
            <data type="anyURI"/>
20
        </element>
21
    </define>
22
</grammar>

ExempleRelaxNG (version 3 : avec datatype, compacte)

1
namespace rng = "http://relaxng.org/ns/structure/1.0"
2
datatypes xsd = "http://www.w3.org/2001/XMLSchema-datatypes"
3
start = AddressBook
4
AddressBook = element addressBook { Card* }
5
Card = element card { Name, Email }
6
Name = element name { xsd:string }
7
Email = element email { text }