XML validation / business rules

Schematron Tutorial for Rule-Based XML Validation

Schematron tests relationships and contextual rules that are awkward or impossible to express as grammar alone. Its XPath assertions can also produce precise, human-readable diagnostics.

Updated2026-07-21
ScopeISO Schematron concepts

Schematron validates XML with rules. A rule selects nodes using an XPath context. Assertions state conditions that must hold for each selected node, while reports identify conditions that do hold. The result is a natural fit for business rules and cross-field constraints.

Layered validation pipeline from XML parsing through XSD and Schematron to an application decision
FIG 03 / Each layer makes a different claim and should retain its own diagnostic report.

Rule-based validation

Grammar validation asks whether a document has an allowed structure. Rule-based validation asks whether selected facts about that document are true. An invoice can have the correct elements and datatypes while still violating a rule that the due date must not precede the issue date.

<sch:pattern id="invoice-dates">
  <sch:rule context="invoice">
    <sch:assert test="xs:date(dueDate) ge xs:date(issueDate)">
      The due date must be on or after the issue date.
    </sch:assert>
  </sch:rule>
</sch:pattern>

Anatomy of a Schematron schema

A schema contains patterns. Patterns group related rules. Each rule has a context, and each assertion or report has a test. Diagnostics can provide reusable detail for messages and downstream reports.

ConstructRole
sch:patternGroups related validation behavior
sch:ruleSelects context nodes
sch:assertFails when its test is false
sch:reportReports when its test is true
sch:diagnosticProvides reusable diagnostic content

Write narrow contexts and readable tests

Prefer a context that selects exactly the nodes governed by a rule. Keep individual tests focused so a failed assertion explains one problem. A single large Boolean expression can be compact but difficult to diagnose and maintain.

Bind namespaces explicitly for the instance vocabulary and verify the XPath version supported by the chosen processor. Unprefixed names in XPath do not automatically match elements in a default XML namespace under common bindings.

Combine XSD and Schematron

  1. Parse the XML and reject malformed syntax.
  2. Apply XSD to validate element structure, attributes and datatypes.
  3. Apply Schematron to validated or typed input for contextual rules.
  4. Apply authorization and external-data policies in the application.

Layering improves error quality. A missing required element is best reported by the grammar validator, while an inconsistent relationship between two present values is well suited to Schematron.

Design reports for the person fixing the data

A useful message names the violated rule, identifies the affected node and explains the expected condition. Avoid exposing the raw Boolean expression as the only explanation. Where the processor supports it, include stable rule identifiers, severity and location information in SVRL output.

Return to XML Schema best practices for grammar design or use the XML syntax checker before rule evaluation.

Query set / FAQ

Frequently asked questions

01What is Schematron?

Schematron is a rule-based validation language for XML. It evaluates XPath assertions in selected contexts and produces messages when assertions fail or reports succeed.

02How is Schematron different from XSD?

XSD describes grammar, datatypes and structural constraints. Schematron expresses assertions about document context and relationships, making it useful for cross-field and business rules.

03Can Schematron replace XML Schema?

It can validate many rules, but it is usually complementary. XSD efficiently validates grammar and types, while Schematron handles contextual assertions and diagnostic messages.

04What does sch:rule context mean?

The context XPath selects the nodes to which assertions and reports apply. Each selected node becomes the context item for the tests inside that rule.

05What is the difference between sch:assert and sch:report?

An assertion produces its message when the test is false. A report produces its message when the test is true. Assertions usually express required conditions; reports identify notable conditions.

06Which XPath version does Schematron use?

The query language binding and processor determine available XPath features. A schema should declare or document its expected binding so deployment does not rely on accidental processor behavior.

07Can Schematron errors include dynamic values?

Yes. Messages can include values and node names selected from the document, allowing diagnostics to identify the actual field or value that violated a rule.

08What is SVRL?

Schematron Validation Report Language is an XML report format commonly produced by Schematron implementations. It records fired rules, failed assertions and successful reports.