- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
In the world of XML validation, there are lots of choices. You can use the original method, Document Type Definitions, which are defined with XML itself, but there are lots of limitations there. You can use XML Schemas, but many developers complain that they're too complicated. You can use RELAX NG, which appears to be gathering steam lately. Or you can use Schematron.
Schematron is an interesting way of validating XML files because it is, quite frankly, not a schema at all. Where the previously mentioned validation methods define a structure for the document and attempt to validate the doucment itself against it, Schematron does no such thing. Instead, Schematron is almost more of a reporting tool, defining situations (based on XPath expressions) in which the tool is to provide either positive or negative feedback regarding the structure of the document.
For example, consider our typical validation target document:
<?xml version="1.0"?> <order orderid="THX1138" customerNumber="3263827"> <lineitem itemid="C33"> <item>3/4" Hex Bolt</item> <quantity>36</quantity> <unitprice currency="dollars">.35</unitprice> </lineitem> <lineitem itemid="M48"> <item>Condenser</item> <quantity>1</quantity> <unitprice currency="dollars">2200</unitprice> </lineitem> <delivery>Overnight</delivery> </order>
We can say several things about this document:
- The order must have an orderid and a customerNumber attribute.
- An order must have at least one lineitem.
- An order must have a delivery method.
- A lineitem must have an itemid attribute.
- A lineitem must have an item, quantity and unitprice child.
- A unitprice must have a currency attribute.
We can also add ideas that have more to do with the business rules involved than the actual data, such as:
- If the order contains more than one lineitem, provide free shipping.
Schematron enables us to create a document that provides information against which the document should be checked. It's based on three basic ideas: patterns, assertions, and reports.
A pattern is used to find the specific information we're checking against. An assertion is much like a validation test. If it fails, the checker outputs a message. A report is the opposite; if its test passes, the checker outputs a message. Let's see how this might work in an actual document. Consider this simple Schematron document:
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.ascc.net/xml/schematron"> <title>Schematron order validation</title> <ns prefix="orders" uri="http://www.example.com/orderSystem"/> <pattern name="Order Structure" id="Order"> <rule context="/order"> <assert test="@orderid">An order must have an associated orderid.</assert> <assert test="@customerNumber">An order must be associated with a customer.</assert> <assert test="delivery">An order must have an associated delivery method.</assert> <report test="lineitem[2]">Customer should receive free shipping.</report> </rule> </pattern> </schema>
Here we've created a single pattern with a single rule intended to check the order element. The rule sets the context, just as an XSLT template's match attribute sets the context in an XSLT style sheet. We've added two assertions and a report, so if we were to remove the customerNumber attribute and validate the file, we'd receive the following message from a tool such as Topologi's XML validator:
Order Structure An order must be associated with a customer. /order[1] <order orderid="THX1138">...</> Customer should receive free shipping. /order[1] <order orderid="THX1138">...</>
In this case the test for the first assertion was true, so the message is not shown. Because we removed the customerNumber, however, the second test comes up false, the equivalent of an error. As a result, the tool outputs the associated message.
For a report, the situation is just the opposite. There are at least two lineitems, so the report test comes up true, and the tool outputs the message.
We can also create multiple rules for a single pattern:
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.ascc.net/xml/schematron"> <title>Schematron order validation</title> <ns prefix="orders" uri="http://www.example.com/orderSystem"/> <pattern name="Order Structure" id="Order"> <rule context="/order"> <assert test="@orderid">An order must have an associated orderid.</assert> <assert test="@customerNumber">An order must be associated with a customer.</assert> <report test="lineitem[2]">Customer should receive free shipping.</report> </rule> <rule context="//lineitem"> <assert test="@itemid">A line item must have an itemid.</assert> <assert test="item">A line item must have an item name.</assert> <assert test="quantity">A line item must have an associated quantity.</assert> <assert test="unitprice">A line item must have a unit price.</assert> <assert test="unitprice/@currency">A unit price must have an associated currency.</assert> </rule> </pattern> </schema>
Or multiple patterns:
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.ascc.net/xml/schematron"> <title>Schematron order validation</title> <ns prefix="orders" uri="http://www.example.com/orderSystem"/> <pattern name="Order Structure" id="Order"> <rule context="/order"> <assert test="@orderid">An order must have an associated orderid.</assert> <assert test="@customerNumber">An order must be associated with a customer.</assert> <report test="lineitem[2]">Customer should receive free shipping.</report> </rule> </pattern> <pattern name="Line Item Structure" id="Item"> <rule context="//lineitem"> <assert test="@itemid">A line item must have an itemid.</assert> <assert test="item">A line item must have an item name.</assert> <assert test="quantity">A line item must have an associated quantity.</assert> <assert test="unitprice">A line item must have a unit price.</assert> <assert test="unitprice/@currency">A unit price must have an associated currency.</assert> </rule> </pattern> </schema>
Schematron also enables you to embed dynamic information within the output. For example, if we were to check for a non-existant attribute, we could provide information not only on where the problem exists within the structure, but within the document:
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.ascc.net/xml/schematron"> <title>Schematron order validation</title> <ns prefix="orders" uri="http://www.example.com/orderSystem"/> <pattern name="Order Structure" id="Order"> <rule context="/order"> <assert test="@orderid">An order must have an associated orderid.</assert> <assert test="@customerNumber">An order must be associated with a customer.</assert> <report test="lineitem[2]">Customer should receive free shipping.</report> </rule> </pattern> <pattern name="Line Item Structure" id="Item"> <rule context="//lineitem"> <assert test="@itemid">A line item must have an itemid.</assert> <assert test="item">A line item must have an item name.</assert> <assert test="quantity">A line item must have an associated quantity.</assert> <assert test="unitprice">A line item must have a unit price.</assert> <assert test="unitprice/@currency">A unit price must have an associated currency.</assert> <assert test="@backordered">The <name /> shown as "<value-of select="item" /> (<value-of select="@itemid" />) " needs a backordered status.</assert> </rule> </pattern> </schema>
Validating against this document provides the following results:
Order Structure Customer should receive free shipping. /order[1] <order orderid="THX1138" customerNumber="3263827">...</> Line Item Structure The lineitem shown as " 3/4" Hex Bolt ( C33 ) " needs a backordered status. /order[1]/lineitem[1] <lineitem itemid="C33">...</> The lineitem shown as " Condenser ( M48 ) " needs a backordered status. /order[1]/lineitem[2] <lineitem itemid="M48">...</>
Schematron also enables you to create different phases, as well as nested reports, and enables you to embed Schematron information within other validation languages such as RELAX NG.