Summary

Rule based validation is at the core of Schematron. For Schematron, rule-based validation means that a document is considered to be valid if it passes a specified set of assertion tests.

An assertion test is a boolean function over a set of nodes. It is associated with zero or more natural-language statements. Schematron uses two elements to represent assertions: An assertion expressed as a sch:assert element succeeds if the assertion test evaluates to boolean true, an assertion expressed as sch:report element succeeds if the assertion test evaluates to false. Both use a test attribute for the assertion test.

A rule is an unordered set of assertions. The assertion elements are grouped into a sch:rule element with a context attribute containing a context expression. The context expression is an expression that selects nodes from the instance document. A rule is said to "fire" if its context expression selects a non-empty set of nodes. All assertions of the rule are evaluated for each node of the set. A single assertion is checked by evaluating the assertion expression with the respective node as context. A rule validates a node if all assertions succeed.

A pattern is an ordered set of rules. The lexical order of the rule elements in the Schematron document acts as an if-then-else switch for nodes of the instance document. A node is checked with at most one rule per pattern.

A schema is an unordered set of patterns. The pattern elements are grouped into a sch:schema element. This element has a queryBinding attribute with an identifier for the language of assertion and context selector expressions.

Originally Schematron was designed as a schema language solely on top of XSLT. The use of XPath and XSLT match expressions was generalized into the concept of a query language in the course of the ISO standardization process. [JELLIFFE2002] [JELLIFFE2003] A query language is defined by a query language binding that specifies the query language for rule context expressions and for assertion tests. The initial 2006 specification defined the query language binding for XSLT 1.0 [XSLT1] as the default query language. The later editions added normative query language bindings for XPath 2.0 [XPATH2], XSLT 2.0 [XSLT2], XPath 3.0 [XPATH3], and XSLT 3.0 [XSLT3].

Assertion tests and context expressions are evaluated during validation. They depend on a properly initialized evaluation environment. Schematron provides two native elements to set up the environment for query language expressions.

The repeatable sch:ns element is a top-level element that defines the available namespace bindings. Namespace bindings have global scope. The repeatable sch:let element defines the available variables and their values. Variables are globally available or scoped to the containing pattern or rule.

Variables and namespaces can be seen as a common denominator of query languages. The 2016 and 2020 updates to the specification, on the other hand, forbid the use of sch:let for XPath 2.0 and 3.0. [1]

Defining namespace bindings and variables is not sufficient to evaluate an XSLT expression. To set up an evaluation environment for XSLT, Schematron explicitly allows the two elements xsl:key and xsl:function as top-level elements.



[1] It is unclear why the working group felt the need to forbid the use of sch:let. All versions of XPath support variable references in XPath expressions.