In the last article, we grouped assertions into rules. As you know, each rule has its own context that it acts upon. So, grouping assertions into a rule provides a way to organize assertions that apply to the same elements. In this article, we'll start by explaining patterns, which takes organization to a new level.
Schematron Patterns and Validation - Validation (Page 3 of 4 )
Now you know almost enough Schematron to create a functional schema and then validate an XML document against it. The only thing left to do is to wrap the schema definition inside of a schema element, which is the root element of a Schematron schema definition. This step is easy:
In order to validate an XML document against a schema, you need a Schematron implementation. There are several implementations of Schematron available, operating in different ways. The simplest implementation is the “skeleton” implementation; it can be used by itself or as a reference for other validators to be built upon. It's written using XSLT, so all that you need to use it is an XSLT transformer. Other validators built upon the skeleton implementation are listed on the Schematron Web site:
One of the simplest validators to start with is Schematron Text. It will validate an XML document against a schema, and then display the result in plain text. So, it's suitable to use in a command line environment. All you need to do is download the XSL file and make sure you have an XSLT transformer. Saxon is a good transformer and is easy to use at the command line. It's available as a JAR or as a .NET assembly:
Note that there are two versions of Saxon: an open source version (Saxon-B) and a commercial version (Saxon-SA). The open source version will work just fine for our purposes. If you're going to use Saxon, then download the open source version unless you intend to obtain a license for the commercial version from Saxonica.
Validation using an XSLT implementation is simple. First, the schema needs to be “compiled” with the XSLT file. A new XSLT file will be generated in the process. Here's how this would be done using Schematron Text, Saxon (the JAR version), and a schema named myschema.sch (.sch is the typical extension for a Schematron schema definition):
Each assertion will be tested, and if an assertion is unmet, then the assertion will be displayed in the console, along with the path to the appropriate node. Note that the skeleton implementation and Schematron Text will not terminate if the validation fails. Instead, validation of the document will continue.