Schematron
Friday 16th September 2022
Friday 16th September 2022
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!
XML has several schema languages available including
The first three of these are all examples of 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
<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>
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>
@contextsch:assert)@test: a relative XPath that can be testederror, warning, informationRules 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.
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/@selectsch:name* Although it would be quite verbose to write a grammar schema using just schematron
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!
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>
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!