Schematron is an XML schema language, and it can be used to validate
XML contents in an XML payload. The following article will talk about
how can we use the Schematron feature in Oracle SOA Suite 11g.
Schematron Basics
Schematron defines a set of rules and checks that are applied to an
XML instance. Schematron takes a unique approach to schemas in that it
focuses on validating document instances instead of declaring a schema
(as the other schema languages do).
Schematron relies almost entirely on XPath query patterns for
defining these rules and checks. With just a subset of XPath, powerful
XSLT stylesheets can be created to process very complex XML instances.
Schematron Structure
A Schematron XML document consist of a schema element in the Schematron namespace: http://www.ascc.net/xml/schematron. The schema element contains one or more pattern
elements.
Pattern elements allow the user to group schema constraints
logically. Some examples of logical groupings are: Text Only Elements,
Valid Root Element, Check for ID Attribute.
Pattern elements have a name attribute. They may also have a see attribute that refers to a URL for user documentation of the schema.
Rule elements define a collection of constraints on a
particular context in a document instance (for example, on an element or
collection of elements). This is very similar to XSLT templates, which
are fired with respect to a node or group of nodes returned by an XPath
expression. If we go back to the XSLT stylesheet we defined earlier:
<xsl:template match='inputVariable'>
The match attribute causes the XSLT processor to evaluate the XPath expression
inputVariable and then instantiate the template relative to the inputVariable element. The contents of a rule element operate within the context of the elements matched by its context attribute.
Rule elements may contain assert and report elements. Both elements are conditionally instantiated depending on the XPath evaluation of their test attribute. The only difference is that assert elements are instantiated if the XPath expressions evaluates to false, while the report elements are instantiated if it evaluates to true. (The general intent is that assert is used to detect errors, while report can be used to report affirmative qualities of an instance.)
The assert/report mechanism is similar to the XSLT xsl:if element in our example stylesheet above, which also has a test attribute that determines if the contents of the xsl:if element are instantiated in the resulting XML tree.
Note that a node can only be the context of a single rule (the first matching rule the processor comes across) within a pattern.
However, a node can be matched multiple times within different
patterns. Thus pattern groupings are important. Every match of a context
node can be considered a discrete constraint.
These elements allow authors of Schematron schemas to provide
functional (and humanly readable) feedback about invalid XML instances.
The user-defined feedback makes Schematron’s unique approach to schema
declaration more powerful than other schema languages.
Finally, assert and report elements have a name element to use for substituting the name of an element into the output stream. The name element has an optional path attribute which returns the node whose tag name will be inserted in place of the name element. If the path attribute isn’t specified the name of the current context node is used instead. This element is often used by assert and report elements to identify the tag name of an offending element within the validation message.
After a Schematron schema is defined, a Schematron XSLT stylesheet is
used to transform the schema to a validating stylesheet. This
stylesheet can then be applied to XML instances for validation purposes.
There are several such Schematron stylesheets, each of which provides
special functionality.