How to validate XML documents against Schematron rules


Table of Contents

Schematron Schema Overview
Creating and editing Schematron schemas
Schematron Schema.
Combined RelaxNG/W3C XML Schemas and Schematron Schema.
Associate Schematron Schema with the document.
Validate against Schematron
From Editor.
On Project tree.

Abstract

This article is intended to show the support for Schematron validation available on <oXygen/> XML editor. You can validate XML documents against Schematron schema or against combined RelaxNG / W3C XML Schema and Schematron.

Schematron Schema Overview

The Schematron is a simple and powerful Structural Schema Language for making assertions about patterns found in XML documents. It relies almost entirely on XPath query patterns for defining rules and checks.Schematron validation rules allow the author to specify a helpful error message which will be provided to the user if an error is encountered. To get more knowledge about it please go to Schematron website http://www.schematron.com/index.html

For validation Oxygen uses the Skeleton XSLT processor and is Schematron 1.5 conformant.

Creating and editing Schematron schemas

Schematron Schema.

In order to define a schema, Oxygen offers you a Schematron editor.

Choose New File and select "Schematron" from the list. A new document is generated and you can start defining rules using the tag-insight mechanism.

Figure 1. Figure 1. Tag insight on a SCH document.

Figure 1. Tag insight on a SCH document.

Any time you can validate the content against Schematron 1.5 XSD Schema using the Validate action. Another way to validate schemas is to check them against their own Schematron schema rules using "External validation" action. For that, select the schematron1-5-schema.sch as Schematron Schema in the "Validate With" dialog.

Combined RelaxNG/W3C XML Schemas and Schematron Schema.

Schematron rules can be embeeded into W3CSchema through annotation (using the appinfo element) or in any element on any level in a RELAX NG Schema (taking into account that the RELAX NG validator ignores all elements that are not in the RELAX NG namespace).

Oxygen accepts such documents as Schematron validation schemas and it is able to extract and use the embeeded rules. To validate a document with both RelaxNG schema and with its embedded Schematron rules you need two persistence associations (see how to set these on the chapter below)

                <?oxygen RNGSchema="percent.rng">
                <?oxygen SCHSchema="percent.rng">
            

The second association will validate your document with Schematron rules extracted from the RelaxNG Schema. Similarly you can specify as Schematron Schema a W3C Schema having the Schematron rules embedded.

                <?oxygen SCHSchema="percent.xsd">
            

Associate Schematron Schema with the document.

If you would like to add a persistance association between your Schematron rules and the current edited XML document, use "Associate Schema" action. A custom PI (process instruction) will be added into the document <?oxygen SCHSchema="percent.sch"?> and the validation process will take into account the Schematron rules.

Besides the simple Schematron schema, you can associate a RELAX NG or XML Schema document having the Schematron rules embeeded inside.

Figure 2. Figure 2. Associate a SCH to an XML document.

Figure 2. Associate a SCH to an XML document.

Validate against Schematron

You can do it in two ways from current editor or through batch validate on project tree.

From Editor.

For one time validation you can use "External Validate" action and set the Schematron Schema to validate against it. (you can also specify a RELAXNG or W3C Schema with rules embeeded).

The possible errors which might occur from validation process are presented on the Result Manager area.

Figure 3. Figure 3. Validate an XML document.

Figure 3. Validate an XML document.

If you validate the XML file often, you can associate the Schematron schema with the document and use the normal Validate button to produce the same result.

If the validation Schematron schema has phases defined, a phase chooser dialog is displayed for you to select the right phase to validate with.

Figure 4. Figure 4. Phase chooser dialog.

Figure 4. Phase chooser dialog.

On Project tree.

Associating Schematron schema into the document is helpful when you want to batch validate couple of XML files using the batch validate feature from the project tree. If the files have the corresponding Schematron schema associated, you can simple select them and use "Validate". The possible errors will be presented on Result Manager.

Figure 5. Figure 5. Validate on project tree.

Figure 5. Validate on project tree.

If the files do not have associated Schematron schemas you can use the "Validate with" feature and set into Schematron Schema tab the corresponding Schematron schema to validate against it (you can also specify a RELAXNG or W3C Schema with rules embeeded)

During the batch validation if Schematron schemas defines phases, the #ALL phase is used by default.