Rule-Based XML Validation with Schematron and XPath
This article provides an overview of Schematron, an ISO-standard validation language designed to enforce complex business rules in XML documents using XPath expressions. Readers will learn how Schematron differs from traditional grammar-based XML schemas, how its core components function, and how XPath enables precise context selection and conditional validation to produce custom, human-readable error reports.
What is Schematron?
Schematron is a structural and rule-based validation language for XML. Unlike traditional grammar-based validation tools such as DTD, XML Schema (XSD), or RELAX NG—which primarily define the structure, sequence, and data types of elements—Schematron focuses on finding patterns and enforcing business logic. Standardized under ISO/IEC 19757-3, it operates by evaluating assertions across the XML document tree to verify that specific data dependencies and semantic constraints are met.
The Role of XPath in Schematron
Schematron relies entirely on XML Path Language (XPath) to navigate XML nodes and evaluate rules. Because XPath allows navigation across any axis (parent, child, sibling, or ancestor), Schematron can evaluate relationships between non-adjacent elements—a task that is difficult or impossible with standard XSD.
Schematron uses XPath in two primary ways:
- Defining Context: Identifying target nodes to be tested.
- Evaluating Assertions: Running boolean expressions against those nodes to check for validity.
Core Components of a Schematron Document
A standard Schematron file is itself an XML document using elements
from the http://purl.oclc.org/dsdl/schematron namespace. It
consists of four main structural layers:
<schema>: The root element containing the entire validation definition, metadata, and namespace declarations.<pattern>: Groups related rules into logical units, such as “Invoice Calculation Rules” or “Metadata Requirements.”<rule context="XPath">: Targets specific nodes using an XPath expression in itscontextattribute. When the validation engine encounters a matching node, it applies the tests nested inside.<assert>and<report>: The testing elements containing boolean XPath expressions in theirtestattributes:<assert test="...">: Generates an error message if the XPath expression evaluates tofalse.<report test="...">: Generates a message if the XPath expression evaluates totrue(useful for flagging prohibited structures).
Example Structure
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern name="Order Validation">
<rule context="Order">
<assert test="TotalAmount = sum(LineItem/Price)">
The TotalAmount must match the sum of all LineItem prices.
</assert>
<report test="ShippingDate and not(PaymentReceived)">
An order cannot have a ShippingDate before PaymentReceived is recorded.
</report>
</rule>
</pattern>
</schema>Key Capabilities of Rule-Based Validation
Using XPath within Schematron unlocks capabilities beyond basic structural validation:
- Co-Occurrence Constraints: Schematron can enforce
rules where the presence or value of one element depends on another
(e.g., “If
Statusis ‘Closed’,ClosingDatemust be present”). - Mathematical and Cross-Field Verification: It can perform arithmetic comparisons across multiple fields, such as verifying totals, discounts, or date sequences.
- Human-Readable Error Messages: When a validation rule fails, Schematron outputs plain-language explanations written directly by the schema author, rather than generic syntax error codes.
- Phased Validation: Schematron supports phases, allowing different subsets of patterns to execute depending on the stage of the document lifecycle (e.g., draft versus publication).
By combining the structural simplicity of XML with the expressive query power of XPath, Schematron serves as a flexible, complementary validation layer that ensures XML data satisfies complex domain requirements.