Schematron
While TREX and RELAX bear a striking similarity to the XML Schema Recommendation, one alternative takes a very different approach: Schematron.
Schematron was developed by Rick Jelliffe, and is not so much an alternative to XML Schemas as a supplemental technology. Jelliffe has described Schematron as "a feather duster that reaches into the corners that the XML Schemas vacuum cleaner can't reach."
What makes Schematron different from the other schema alternatives is that Schematron is not grammar-based. Instead, it's rules-based, and therefore provides a level of validity checking that's simply not possible with XML Schemas.
For example, Schematron allows you to check the content of elements and attributes, and then check for dependencies based on those results. Schematron also provides a mechanism for returning "plain English" error messages that are easily understood.
Let's say you had the following XML instance:
<employee SSN="331001234">Jane Doe</employee>
Schematron would allow you to check to make sure that the employee element had the SSN attribute. It would also allow you to count the digits in the SSN, as well as check to make sure that a name was included for the element content. And if there was a problem, you could specify an error message such as this:
"Social Security Numbers should be exactly 9 digits"
Schematron is very flexible. In fact, it can be used not only with XML Schemas, but with RELAX NG grammars! As if that weren't enough, Schematron is also very simple. There are only six basic elements in Schematron, and the technology is built using XSLT and XPath. So if you already know XPath and XSLT, Schematron can be learned very quickly. Schematron works by using XPath to specify the location and conditions that are being checked, and it uses XSLT to perform the manipulation of the Schematron Schema and the application of the Schematron Schema to your instance documents.
Another advantage of Schematron is that because it is based on core XML technologies, Schematron Schemas can actually be nested directly within XML Schemabased schemas, using annotation and appinfo elements.
Because of the fundamentally different approach, Schematron Schemas look very different from XML Schemas and RELAX NG:
<?xml version="1.0" encoding="UTF-8" ?> <schema xmlns="http://www.ascc.net/xml/schematron"> <title>An Example Schematron Schema for Business Car</title> <pattern name="Root Element"> <rule context="/*"> <assert test="name()='card'">Root element should be card.</assert> </rule> </pattern> <pattern name="Count Elements"> <rule context="/*"> <assert test="count(name) = 1">There should be 1 name element.</assert> <assert test="count(title) = 1">There should be 1 title element.</assert> <assert test="count(company) = 1">There should be 1 company element.</assert> <assert test="count(email) = 1">There should be 1 email element.</assert> </rule> </pattern> <pattern name="Check the Phone Number Element"> <rule context="phone"> <report test="string-length(phone) < 10">Please check the phone number and make sure it is at least 10 digits</report> </rule> </pattern> </schema>
Schematron is an excellent choice for a supplementary technology to provide a finer level of validation than possible with XML Schemas.
Ultimately, the schema choices you make will depend on a variety of factors, including your comfort level with the support provided by and for alternate schemas, as well as your desire for compatibility with other softwareand most importantly the needs of your documents. The important fact to take away is that there are always options, and XML Schemas may be right for some, while RELAX NG or Schematron may be right for others.