Let's Start Ruling

You're probably impatient to see Schematron at work and want to start defining your first Schematron rules right now.

Setting the Context

Rules are the basic building block of Schematron schemas. Like XSLT templates, Schematron rules set the "context node" from which relative expressions are evaluated. They also act as containers to create groups of tests.

Warning

Although rules are similar to XSLT templates in the way that they set context nodes, you will see in a later section that the algorithm used to run across a document and select the rule matching each node is totally different from the algorithm used by XSLT to apply and select templates.

To specify which nodes a rule must be applied on, use the context attribute, as in the following:

<rule context="book">
<!--
Insert tests to be done on "book" elements here
-->
.../...
</rule>

The XPath expression in the context attribute can be more complex than this, and it can include all the constructions allowed in an XSLT pattern. (XSLT patterns are the XPath expressions that are allowed in match attributes.)

We could, for instance, write context="book[@id]" to define a rule that applies only to book elements with an id attribute, or write context="*[@id]" to define a rule that applies to any element with an id attribute, or write context="library/book" to define a rule that applies to book elements with a library parent element.

Warning

Schematron 1.5 applies rules to the following node types:

  • Elements (*)

  • Text nodes (text() ...

Get Schematron now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.