Extending XML Document Validation with Schematron

0 0
Read Time:4 Minute, 44 Second

The use of XML documents is common practice nowadays and so is XML schema (XSD) to validate XML documents. XML validation is often needed to ensure structure, content and relations are correct and valid. However validation only using a schema (XSD) only covers a small part: it can describe the basic XML structure (valid elements and order) and some basic content validation of a XML node. Schematron can be used to cover the remaining part of XML validation, like:

  • Advanced structure validation
    e.g. element A should have either attribute X or attribute Y, but not both and always one of them
  • Structure depending on content
    e.g. when attribute A of element B has value ‘x’ , then it should have child element C
  • Content validation on multiple nodes
    e.g. sum of all percentage elements should be 100
  • Relations between elements
    e.g. For each employee element with a manager attribute there should be another employee element with an id attribute having the same value (meaning manager of employee should exist)

Schematron, just like an XSD, is an XML document itself. Each validation rule is defined by a rule element. The rule element has a context attribute to define on which node (or nodes) of your target XML the rule applies to. A X-path expression is used to define this context. For example a XML document containing one or more Department elements and we want to define a rule for each Department element in the XML document:

A rule element has one or more report and/or assert elements. Both contain a test attribute with the actual validation rule, the test. The only difference between them is that a report element results in an output error when the test results in (boolean) true whereas the error element results in an output error when the test results in (boolean) false.
For the test attribute also an X-path expression is used to define the validation rule.
Let’s say our Department element has two attributes, “name” and “abbr” (abbreviation) and two business rules apply:

  1. abbr should contain at least two characters
  2. abbr should contain less characters then name

Defining these rules with Schematron results in: (in xml < character is written as &lt;)

  Abbreviation too short
  Abbreviation too long

To complete the Schematron XML document, a rule element is a child element of the pattern element. The pattern element is used to group rules and a provide a name for the group. It’s only for readability and it has no further technical meaning. With root element schema we can finish the Schematron document.
The complete Schematron document of our little example:

< ?xml version="1.0" encoding="UTF-8" ?>

      Abbreviation too short
      Abbreviation too long

Below another source example with the rule that the total sum of Percent elements somewhere (childs or even grandchilds) within a Total element should be 100.

< ?xml version="1.0" encoding="UTF-8" ?>

               Sum is not 100%.

Before continuing with a complex example with relations between elements, how do we get this to work?
In fact, that’s quite easy. You only need to be able to do xsl(t) translations!
The beauty about Schematron is that it’s not a new technology, but just clever usage of xslt translations. No new language is needed and you even don’t need to learn xslt, just basic knowledge of X-path and XML are sufficient.

The trick is that you have to transform your Schematron XML containing your validation rules into a xslt that contains your validation rules. Then you use this generated xslt for validation of the XML documents by doing a xslt translation. And how do you generate your rules xslt from your Schematron document…also by an xslt translation! Your rules xslt is the result of the translation of your Schematron XML (with your validation rules) with a provided Schematron xslt (iso_schematron_skeleton_for_xslt1.xsl or iso_schematron_skeleton_for_xslt2.xsl, downloadable from schematron.com).
So it’s a two step approach. First you translate your Schematron rules XML with the Schematron xslt resulting into a new xslt. This xslt contains your rules. Now you can use your generated xslt to validate xml documents by doing a xslt translation. This final translation results into your errors or no output when the validation succeeds.

Schematron two step validation proces

In a production environment most of the time the rules are predefined or do not change (often), so the generated xslt can be stored (or cached).

To show the possibilities of Schematron validation I finalize this blog with the promised complex example with element relation rules.
Let’s start with the target XML, so the XML data which has to be validated. With example data it is easier to understand the Schematron rules.

< ?xml version="1.0" encoding="UTF-8" ?>

        J. Jansen
        P. Klaasen
        M. A. Neger
        L.E. Ader
        P.R. Esident

We want to implement the following business rules:

  • All employees of department “The Floor” should have less salary than any manager (=employee in department “Managers”).
  • An employee may not be the manager of himself.
  • There is only one manager without a manager (only one president).
  • The relation manager and employee is a valid one, so the manager of an employee must exist. This means that for each employee with a manager attribute there must be a manager with attribute id with the same value.

In Schematron xml these rules result into:

< ?xml version="1.0" encoding="UTF-8" ?>

      Too much
      Own manager
      More than one president
      Not a valid manager

More information can be found at schematron.com.
An easy step by step Schematron tutorial can be found here.


About Post Author

Emiel Paasschens

Emiel is a Solutions Architect and a Java & Integration consultant in The Netherlands.
0 %
0 %
0 %
0 %
0 %
0 %

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

4 thoughts on “Extending XML Document Validation with Schematron

  1. Chris, you are right, performance can be an issue. Aside from the hardware, the performance is much dependent on the size en complexity of the xml file and the amount and complexity of the Schematron rules (thus size and complexity of the final xslt). For a big insurance company we ‘ve implemented business validation of an insurance policy. The performance trick was to cache (in java) the binary result of the final xslt compilation. So for a validation only one translation has to be done, where this translation had already been compiled.

  2. One of the main reasons most places don’t do plain schema validation in production is the performance impact.  How does this impact performance?  This looks like a really nice tool if it performs well.

  3. There are lots of use cases or reasons to use Schematron.  A real-world example. For an insurance company I’ve written a validation webservice. Incoming parameters where an xml document reflecting an insurance application, name of the insurance application type and version. The application had to be validated against lots of specific insurance business rules. The webservice first validates the document against an xsd and after succesful parsing it was validated against the insurance bussines rules using Schematron. Schematron was choosen because the rules changes quite often. So these rules (in Schematron xml) were stored in a db and the webservice generated (and cached) the schematron xslt after changes in the rules (and also in case of a new version) .

  4. Hi Emiel,
    Nice article.
    Schematron can be a good option if requirement says fixed business rules to be enforced on data flow.
    Sumit Dnyanesh Tambekar

Comments are closed.

Next Post

AMIS Query: An Evening with Oracle ACE Director Doug Burns...

On the 18th of June I am very happy to have arranged an internal mini masterclass for my DBA colleagues and some AMIS customer DBA peers. The evening before on the Thursday the 17th of June (btw. no important World Cup soccer games going on during this Thursday evening) Doug […]
%d bloggers like this: