Schematron's Six Basic Elements
There are only 6 basic elements in ISO Schematron which makes it very easy to learn, especially if you already know XPaths. (There are others, but these mainly just help construct nice user interfaces for validators.) Here is the basic structure
- <schema xmlns="http://purl.oclc.org/dsdl/schematron"> contains
- optional <title> then
- zero or more <ns prefix="???" uri="???" /> giving the namespaces and prefixes used for the XPaths, then
- several <pattern>, which
contain
- several <rule context="???">
where the context attribute is an XSLT expression,
which contain mixed
- <assert test="???"> where the testattribute is an XPath location, and which contains rich text expressing the statement being asserted in plain language, and
- <report test="???"> where the testattribute is an XPath location, and which contains rich text expressing the fact to be reported in plain language.
- several <rule context="???">
where the context attribute is an XSLT expression,
which contain mixed
So here is a very small example. It is a mini-schema for Schematron.
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<title>A Schematron Mini-Schema for Schematron</title>
<ns prefix="sch" uri="http://purl.oclc.org/dsdl/schematron">
<pattern>
<rule context="sch:schema">
<assert test="sch:pattern"
>A schema contains patterns.</assert>
<assert test="sch:pattern/sch:rule[@context]"
>A pattern is composed of rules.
These rules should have context attributes.</assert>
<assert test="sch:pattern/sch:rule/sch:assert[@test]
or sch:pattern/sch:rule/sch:report[@test]"
>A rule is composed of assert and report statements.
These rules should have a test attribute.</assert> </rule>
</pattern>
</schema>
In that mini-schema, the rule element sets the context: the rule applies to any sch:schema element in a document. The rules say that there must be at least one child element sch:pattern, at least one child element sch:pattern with a child sch:rule with a context attribute, and at least one child element sch:pattern with a child sch:rule with a sch:assert or sch:reportwith a test attribute.
This is probably not the most useful schema: it only tells you what is wrong with an empty document rather than checking a full Schematron schema. However, it does show that there are many different kinds of schemas possible: some of them similar to DTDs and some of them very different. Schematron lets you perform many kinds of new validation!
Please note that as of 2004, existing implementations of Schematron use the pre-ISO namespace http://www.ascc.net/xml/schematron.
