XML technologies / DocBook / Customizing DocBook / DocBook 5 structure / Schematron

(Excerpt from "DocBook-XML: Media-neutral and platform-independent publishing" by Thomas Schraitle)

Compared with the DTD, DocBook 5 includes an additional schema language called Schematron, developed by Rick Jelliffe. DTDs, RELAX NG, and W3C Schema focus their content models on parent-child relationships. Schematron, by contrast, resembles XSLT/XPath and describes validation rules using so-called assertions (English: assertions). Usually, Schematron is not used alone, but supplements an existing RELAX NG or W3C schema. (Its inventor, Rick Jelliffe, therefore describes Schematron as “a feather duster for reaching parts that other schema languages cannot reach.”)

Note: Do I need Schematron?
Validation incorporating Schematron provides an additional safeguard against errors. However, a DocBook 5 document can also be validated without Schematron. This is particularly useful when your XML parser does not support this schema language. You must then ensure yourself that all constraints are satisfied. All constraints are contained in the file docbook.sch.

Schematron consists mainly of Schematron rules. A rule usually consists of two components:

  1. The context: Each rule identifies the object to be checked, usually an element or attribute in XPath notation.

  2. The assertion: This checks whether a particular assumption holds. It is also expressed in XPath.

Both components are contained in an s:pattern element. For DocBook 5, this means that Schematron patterns are embedded in the RELAX NG and W3C schemas. For the element book this looks as follows in the RELAX NG schema:

Example: Schematron pattern for version in book

RNC

namespace s = "http://www.ascc.net/xml/schematron"
db.book =
  ## A book
  [
    s:pattern
      name = "Root must have version"
      s:rule
      context = "/db:book"
      s:assert
        test = "@version"
        "The root element must have a version attribute."
      ]
  ]
 ]
]

RNG

<element name="book" xmlns:s="http://www.ascc.net/xml/schematron">
    <s:pattern name="Root must have version">
        <s:rule context="/db:book">
            <s:assert test="@version">The root element must have a version attribute.</s:assert>
        </s:rule>
    </s:pattern>
</element>

Line 1, RNC and RNG: The namespace of Schematron v1.5.
Line 2, RNC, and line 1, RNG: A Schematron pattern is specified in the RELAX NG schema for the element book.
Line 5, RNC, and line 2, RNG: The Schematron pattern contains an s:rule and an s:assert.
Line 7, RNC, and line 3, RNG: The Schematron rule matches all book elements in the DocBook 5 namespace that appear as the document’s root element. This defines the context to which all assertions refer.
Line 9, RNC, and line 4, RNG: test checks whether a book element has a version attribute. If the attribute is missing, the corresponding message is displayed.

This section has only scratched the surface of the subject. For more detailed information, see the Schematron home page.

  

back to top