Schematron

Friday 16th September 2022

Validation and Schemas

Defining a Mark-up Language

We have seen how XML allows the creation of custom Mark-up applications* using nodes, and how to use namespaces to avoid naming clashes.

This may be fine if only you are going to use the format, but that's rarely the case!

Schema
languages define the rules for your application; which elements are allowed where, and what they are allowed to contain.
Valid
XML documents are ones which follow the rules of a given Schema
Validation
is the process of testing to see whether a (number of) XML instances are valid.

Schema Languages

XML has several schema languages available including

The first three of these are all examples of Grammatical schema.

Grammatical Schema

This type of schema is a grammar* for an XML language. Most XML applications use one of them to define:

Some schema languages also let us define:

*cf. EBNF

Schematron

What is Schematron?

A Schematron Sample


      <sch:schema queryBinding="xslt2">
        <sch:ns uri="http://www.xmlsummerschool.com/example/ns" prefix="d"/>
        <sch:pattern id="eg1">
          
          <sch:rule context="d:fn">
            <sch:report test="ancestor::d:fn" role="error">Footnotes aren't allowed to contain footnotes!</sch:report>
          </sch:rule>
          
          <sch:rule context="d:date">
            <sch:assert test=". castable as xs:dateTime" role="error"><sch:name/> must be a dateTime!</sch:assert>
            <sch:assert test="hours-from-duration(timezone-from-dateTime(.)) eq 0" role="warning"><sch:name/> must use the UTC timezone!</sch:assert>
          </sch:rule>
          
        </sch:pattern>
      </sch:schema>
    

Asserting Rules

Most schematron rules assert that a given test is true:


      <sch:rule context="d:date">
        <sch:assert test=". castable as xs:dateTime" role="error"><sch:name/> must be a dateTime!</sch:assert>
    </sch:rule>
    

Reporting Rules

Rules can also report that a given test is false:


      <sch:rule context="d:fn">
        <sch:report test="ancestor::d:fn" role="error">Footnotes aren't allowed to contain footnotes!</sch:report>
      </sch:rule>
    

Otherwise identical to sch:assert.

Writing messages

Grammar based schema throw pre-defined errors; they can sometimes be hard to interpret.

Schematron allows you to write your own! This has several key advantages:

There are also a number of features you can use to dynamically formulate the messages:

sch:value-of/@select
Allows you to print the value of any XPath statement
sch:name
Uses the name of the context (so you can re-use rules in different contexts)

Advantages of Schematron

* Although it would be quite verbose to write a grammar schema using just schematron

XSpec and Schematron

Testing Schematron with XSpec

Schematron rulesets can get very large, and take a lot of maintenance.

Changes of requirements, or special exceptions to rules can break working rules

How can we mitigate this risk? Write behavioural tests for our schematron!

Instructions are available on Github

XSpec Syntax

The root element of every XSpec document is still x:description.

The schematron being tested can be specified using a relative URI as the value of @schematron (rather than @stylesheet).


      <x:description schematron="filename.sch">
          ...
      </x:description>
    

More Expectations

We get to use new expect values to test our rules;

@id refers to the rule ID

@location refers to the XPath location of the x:context.


      <x:expect-assert id="a0001" location="/html[1]/body[1]/div[5]"/>
    

Will throw an error if the given assertion is not triggered at the given node


      <x:expect-not-assert id="a0001" location="/html[1]/body[1]/div[4]"/>
    

It's a good idea to have both a positive and a negative test for when the rule should and shouldn't fire!


      <x:expect-report id="a0002" location="/html[1]/body[1]/div[6]"/>
      <x:expect-not-report id="a0002" location="/html[1]/body[1]/div[7]"/>
    

Elements are also available for report based rules!