RelaxNG Schema

Figure 745. XML grammar defining languages Slide presentation
  1. REgular LAnguage for XML Next Generation (RelaxNG)

  2. Schematron

  3. XML Schema (XSD)

  4. Document Type Definition (DTD)


Figure 746. Address list schema Slide presentation
Schema Doc instance
<element name="aBook">
  <zeroOrMore>
    <element name="person">
      <element name="fullName">
        <text/>
      </element>
      <element name="email">
        <text/>
      </element>
    </element>
  </zeroOrMore>
</element>
<aBook>
  <person>
    <fullName>Jim Bone</fullName>
    <email>bone@mycity.com</email>
  </person>
</aBook>

exercise No. 5

Inventing a <book> grammar

Q:

In this exercise we develop a simple grammar aiming at book style documents.

Read the RELAX NG Tutorial. You may want to import the examples into your OxygenXML IDE. Based on this knowledge you ar being asked to model a schema describing simple <book> documents.

Use Oxygen XML Editor to create a RelaxNG book.rng schema suiting the subsequent document sample:

<book lang="en">
   <title>My first book</title>
   <chapter>
       <title>Introduction</title>
       <paragraph>Some text.</paragraph>
       <paragraph>More text.</paragraph>
   </chapter>
    <chapter>
        <title>Go on ...</title>
        <paragraph lang="de">Hier kommt noch etwas.</paragraph>
    </chapter>

    <chapter>
        <title>Complex examples</title>
        <itemizedlist>
            <listitem>
                <paragraph lang="es"/>
            </listitem>
            <listitem>
                <itemizedlist>
                    <listitem>
                        <paragraph>One</paragraph>
                    </listitem>
                    <listitem>
                        <paragraph>Two</paragraph>
                        <paragraph>Two and more</paragraph>
                    </listitem>
                </itemizedlist>
            </listitem>
        </itemizedlist>
    </chapter>
</book>

The following rules shall be obeyed:

  • Valid documents must start with <book>.

  • A <book> element contains a <title> followed by at least one <chapter>.

  • Each <chapter> has (exactly one) a <title> which is being followed by at least one <paragraph> or <itemizedlist>.

  • The elements <title> and <paragraph> consist of ordinary text.

  • Both <book> and <para> elements may have an optional lang attribute like en, fi, es, it and so on. Use the data type NMTOKEN.

  • <itemizedlist> elements consist of at least one <listitem> element.

  • <listitem> elements consist of least one <paragraph> or <itemizedlist>.

You may want to start from the following file mybook.rng:

<?xml version="1.0" encoding="UTF-8"?>
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <ref name="d.book"/>
    </start>

    <define name="d.book">
        <element name="book">
            <text/>
        </element>
    </define>

    <!-- TODO: extend me accordingly ... -->
</grammar>