Schematron

Schematron, which was developed by Rick Jelliffe, is a simple yet powerful schema language for XML and has recently become a an ISO standard candidate (ISO/IEC 19747-3; see http://www.dsdl.org). Schematron uses rule-based validation rather than the grammar-based validation used by XML Schema and RELAX NG, among others. It uses expressions written in XPath to precisely examine nodes in an instance, thus becoming, as Jelliffe puts it, the “feather duster” that can reach into corners where grammar-based languages cannot. Schematron is good at testing for co-occurrence constraints—that is, constraints based on the existence of a value or structure that in turn is based on the existence of another value or structure.

The most common version of Schematron is 1.5. You can obtain a reference implementation (an XSLT stylesheet) for Version 1.5 from http://xml.ascc.net/schematron/1.5/. A variety of Schematron validators are available from Topologi (http://www.topologi.com/). You can also get information on the new ISO Schematron and its implementations from http://www.schematron.com/.

An example of a Schematron 1.5 schema is shown in Example 1-13.

Example 1-13. horse.sch
1 <?xml version="1.0" encoding="US-ASCII"?> 2 <sch:schema xmlns:sch="http://www.ascc.net/xml/ schematron"> 3 <sch:title>Horse schema</sch:title> 4 <sch:pattern> 5 <sch:rule context="horse"> 6 <sch:assert test="@legs = '4'">Our horses should have 4 legs.</sch:assert> 7 <sch:assert test="snip">Our horses should have ...

Get XML Pocket Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.