Book documents

exercise No. 10

A schema for editing books Create comment in forum

Q:

Write a schema describing book document instances with the following features:

  • A book shall have a title to describe the book itself.

  • A book shall have at least one but possibly a sequence of chapters.

  • Each chapter shall have a title and at least one paragraph.

  • The titles and paragraphs shall consist of ordinary text.

A:

A possible schema looks like:

Figure 761. A first schema version for book documents
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
   vc:minVersion="1.0" vc:maxVersion="1.1">

   <xs:element name="book">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="title"/>
            <xs:element ref="chapter" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:element name="title" type="xs:string"/>
   <xs:element name="chapter">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="title"/>
            <xs:element ref="para" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:element name="para" type="xs:string"/>

</xs:schema>

We supply a valid document instance:

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="book.xsd">
  <title>Introduction to Java</title>
  <chapter>
    <title>Introduction</title>
    <para>Java is a programming language</para>
  </chapter>
  <chapter>
     <title>The virtual machine</title>
     <para>We also call it the runtime system.</para>
  </chapter>
  <chapter>
    <title>Annotations</title>
    <para>Annotations provide a means to add meta information.</para>
    <para>This is especially useful for framework authors.</para>
  </chapter>
</book>

.

exercise No. 11

Book documents with mixed content and itemized lists Create comment in forum

Q:

Extend the first version of book.xsd to support the following features:

  • Within a <chapter> node <para> and <itemizedlist> elements in arbitrary order shall be allowed.

  • <itemizedlist> nodes shall contain at least one <listitem>.

  • <listitem> nodes shall be composed of one or more para or <itemizedlist> elements.

  • Within a <para> we want to be able to emphasize text passages (mixed content).

The following sample document instance shall be valid:

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="book.xsd">
  <title>Introduction to Java</title>
  <chapter>
    <title>Introduction</title>
    <para>Java supports <emphasis>lots</emphasis> of concepts:</para>
    <itemizedlist>
      <listitem>
        <para>Single <emphasis>implementation</emphasis> inheritance.</para>
      </listitem>
      <listitem>
        <para>Multiple <emphasis>interface</emphasis> inheritance.</para>
        <itemizedlist>
          <listitem><para>Built in types</para></listitem>
          <listitem><para>User defined types</para></listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </chapter>
</book>

A:

An extended schema looks like:

Figure 762. Version 2 of book.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <!-- Elements having no inner structure (but maybe attributes) -->
    <xs:element name="title" type="xs:string"/>
    <xs:element name="emphasis" type="xs:string"/>

    <!-- Starting the game ... -->
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="title"/>
                <xs:element ref="chapter" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="chapter">
        <xs:complexType>
            <xs:sequence> <xs:element ref="title"/>
                <xs:choice minOccurs="1" maxOccurs="unbounded">
                    <xs:element ref="para"/>
                    <xs:element ref="itemizedlist"/>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="para">
        <xs:complexType mixed="true"> <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="emphasis"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>

    <xs:element name="itemizedlist">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="listitem" minOccurs="1" ❸ maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="listitem">
        <xs:complexType>
            <xs:choice minOccurs="1" maxOccurs="unbounded"> <xs:element ref="para"/>
                <xs:element ref="itemizedlist"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>

</xs:schema>

This allows emphasized text in <para> nodes and <itemizedlists>.


We hook into <chapter> to allow arbitrary sequences of at least one <para> or <itemizedlist> element node.

<para> nodes now allow for mixed content.

An <itemizedlist> contains at least one list item.

A <listitem> contains a sequence of at least one <para> or <itemizedlist> child node. The latter gives rise to nested lists. We find a similar construct in HTML namely unnumbered lists defined by <ul><li>... constructs allowing for nested elements as well.

exercise No. 12

book.xsd and languages Create comment in forum

Q:

We want to extend our schema from Q: by allowing an author to define the language to be used within the whole or parts of the document in question. Add an attribute lang to all relevant elements like e.g. <para lang="es">. An XML editor may use this attribute to activate corresponding dictionaries for spell checking.

The lang attribute shall be restricted to the following values:

  • en

  • fr

  • de

  • it

  • es

A:

We define a suitable <xs:attribute> type:

<xs:attribute name="lang">
  <xs:simpleType>
    <xs:restriction base="xs:string">
        <xs:enumeration value="en"/>
        <xs:enumeration value="fr"/>
        <xs:enumeration value="de"/>
        <xs:enumeration value="it"/>
        <xs:enumeration value="es"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

Than we add this attribute to our elements like <chapter> and others:

    <xs:element name="chapter">
        <xs:complexType>
            <xs:sequence> ... </xs:sequence>
            <xs:attribute ref="lang" use="optional"/>
              ...
        </xs:complexType>
    </xs:element>

This allows us to set a language on arbitrary hierarchy level. But of course we may define it on top level as well:

<?xml version="1.0" encoding="UTF-8"?>
<book ... lang="english">
  <title>Introduction to Java</title>
...

exercise No. 13

Supporting <table> and internal references in book.xsd. Create comment in forum

Q:

We want to extend book.xsd and languages schema to allow for HTML style tables and document internal references:

  • Provide a simplified HTML table model which supports <table border="..." id="...">, <caption>, <tr> and <td colspan="..." rowspan="..."> elements (all attributes being optional) e.g.:

    ...
    <table>
        <caption>This is a table</caption>
        <tr>
            <td>Value 1</td><td>Value 2</td>
        </tr>
        <tr>
            <td colspan="2">Spanning a complete line</td>
        </tr>
    </table> ...

    <table border="..." id="..."> elements shall be treated on an equal footing as <para> and <itemizedlist> elements.

  • Allowing all <chapter>, <para>, <itemizedlist> and <table> to become reference targets.

  • Extending the element para's mixed content model by a new element <link> with target referencing attribute <link linkend="..."> of type xs:IDREF.

A:

We extend our schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
                     elementFormDefault="qualified"
    vc:minVersion="1.0" vc:maxVersion="1.1">

    <xs:import namespace="http://www.w3.org/XML/1998/namespace"
            schemaLocation="http://www.w3.org/2001/03/xml.xsd" />


    <xs:include schemaLocation="table.xsd"/>

    <!-- Type definitions -->

    <xs:attribute name="lang">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="en"/>
                <xs:enumeration value="fr"/>
                <xs:enumeration value="de"/>
                <xs:enumeration value="it"/>
                <xs:enumeration value="es"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>

    <!-- Elements having no inner structure -->
    <xs:element name="emphasis" type="xs:string"/>
    <xs:element name="title" type="xs:string"/>

    <xs:element name="para">
        <xs:complexType mixed="true"> 
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="emphasis"/>
                <xs:element ref="link"/>
            </xs:choice>
            <xs:attribute ref="lang" use="optional"/>
            <xs:attribute name="id"  type="xs:ID" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="link">
        <xs:complexType>
            <xs:simpleContent>
               <xs:extension base="xs:string"> 
                   <xs:attribute name="linkend"  type="xs:IDREF" use="required"/>
               </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

    <!-- Starting the game ... -->
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="title"/>
                <xs:element ref="chapter" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute ref="lang" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="chapter">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="title"/>
                <xs:choice minOccurs="1" maxOccurs="unbounded">
                    <xs:element ref="para"/>
                    <xs:element ref="itemizedlist"/>
                    <xs:element ref="table"/>
                </xs:choice>
            </xs:sequence>
            <xs:attribute ref="lang" use="optional"/>
            <xs:attribute name="id"  type="xs:ID" use="optional"/>
            <xs:attribute ref="xml:base"/> <!-- This allows supporting <xi:include ...> -->
        </xs:complexType>
    </xs:element>

    <xs:element name="itemizedlist">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="listitem" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute ref="lang" use="optional"/>
            <xs:attribute name="id"  type="xs:ID" use="optional"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="listitem">
        <xs:complexType>
            <xs:choice minOccurs="1" maxOccurs="unbounded">
                <xs:element ref="para"/>
                <xs:element ref="itemizedlist"/>
            </xs:choice>
            <xs:attribute ref="lang" use="optional"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

A <link> is part of the element <para>'s mixed content model: An author may define ordinary text pointing to a document internal target reference.

Like the <a> tag in HTML a <link> may contain text. If converted to HTML the formatting expectation is a hypertext link.

The linkend attribute holds a reference of type IDREF to an internal target being either a <chapter>, a <para> or an <itemizedList>.

Defining an attribute id of type ID for the elements <chapter>, <para> and <itemizedList>. This enables an author defining internal reference targets.

The table.xsd schema module mimics the HTML table model:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="table">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="caption" type="xs:string"/>
                <xs:element ref="tr" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:ID" use="optional"/>
            <xs:attribute name="border"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="tr">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="td" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="td">
        <xs:complexType mixed="true">
            <xs:attribute name="colspan" type="xs:positiveInteger" use="optional"/>
            <xs:attribute name="rowspan" type="xs:positiveInteger" use="optional"/>
        </xs:complexType>
    </xs:element>

</xs:schema>