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:
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
...
</schema>
Now the schema is ready for use.
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:
http://www.schematron.com/validators.html
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:
http://saxon.sourceforge.net/
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):
java -jar saxon9.jar -o myschema-compiled.xsl myschema.sch iso_schematron_text.xsl
This will create the file myschema-compiled.xsl which can then be used to validate an XML document against the schema. Here's how you would validate a document called doc.xml using the schema:
java -jar saxon9.jar doc.xml myschema-compiled.xsl
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.
Next: Conclusion >>
More XML Articles
More By Peyton McCullough