An example Schematron schema for validating W3C XSD

by Rick Jelliffe

Regular grammars, as used by W3C XML Schemas (XSD), are very good for representing some kinds of patterns in documents. XPaths, as used by ISO Schematron, is very good for locating and testing many other kinds of patterns. One of the reasons that the XML Schemas specification is so difficult is that, after the "XSD schema for XSD schemas" has been taken into consideration, there are still many more constraints left over; and these have to be written up in natural language. The result is that developers and implementers of XSD are left without a standard executable validator.

Here is how you would do it in Schematron. In fact, you could typeset the schema into a useable specification, because it allows rich text and various kinds of linking. Download file

The schema only has a couple of constraint sets, from the several dozen required, but it shows the kinds of thing that Schematron is good for. If you are making up a schema for a public specification, for example and industry standard, and you are finding you have more than a handful of constraints that cannot be expressed in W3C XML Schemas, consider formalizing them in an ISO Schematron schema. The XPaths not only clarify the meaning of the natural language, but they also allow validation with a Schematron validator (which is usually built from a couple of XSLT scripts.)