Contact details

xchecker 0.9.2

Download xchecker
1. What is xchecker?
xchecker is both a test framework for XML and XSLT, and a wrapper for an XML Schema processor that allows XML Schema documents to be augmented with xchecker checks.
XML Schema, XPath 2.0, XSLT 2.0, XQuery and Relax NG can be combined to check an XML document, or the XML result of an XSLT transform.
Augmenting schema checks with XPath/XSLT checks gives you the power to fully check the correctness of an XML document or transform - XML Schema or Relax NG alone aren't sufficient to fully constrain an XML document, but when used in combination with XSLT / XPath it's possible to check anything - like having a grammar and rules based checker in one.
xchecker can run from the command line, from ANT, or from Java using its API/JAXP schema interfaces.
See the samples dir and the Readme for more examples.
2. How does it work?
The xchecker processor evaluates each check and looks for a result of 'true' - any other values are treated as failures. This means the failure message can be constructed by the test writer and be as descriptive as needed, using the source XML and the expressive power of XPath/XSLT/XQuery. The complexity of the check, and the quality of the failure message aren't limited in any way.
XPath 2.0 checks can return a sequence of items - any item not equal to 'true' is considered a test failure and the value returned to the user as the failure message.
The schema checks are considered a pass if the XML validates, and false if any validation errors are returned. It's intended that the schema checks be used in combination with XPath/XSLT/XQuery checks to fully check the correctness of the XML.
3. Using xchecker as a test framework
Checks are written in a "Check Config" file:
<!-- A simple "Check Config" which checks XML document using an XML Schema, a couple
     of XPaths and an XSLT file.  Paths are all relative to this config.
<xchecker xmlns="">
<xml src="xml/books.xml">
<!-- The XML Schema for the books.xml -->
<!-- Checks all Thomas Hardy books are published in a "Classic" range -->
<check>not(exists(/booklist/books/item[author eq 'Thomas Hardy'][not(contains(publisher, 'Classics'))]))</check>
<!-- The same check but written in XSLT with better error messages -->
<!-- Check each book's category has a coresponding entry in the categories section --> 
<check>if (/booklist/books/item/@cat[not(. = /booklist/categories/category/@code)]) then
Category not found: ', /booklist/books/item/@cat[not(. = /booklist/categories/category/@code)])
    else 'true'
(books.xml is copied from the samples dir of the Saxon download)
Here the test subject is "books.xml" and there are 4 checks applied - an XML Schema, two XPaths and an XSLT check.
For the first check the XML is validated using the schema and the result is stored. If the XML is valid according to the schema the result for that check is a pass, if an error is returned then the result is that error message.
The second check is an XPath which checks a co-occurrence constraint - that all elements with an of "Thomas Hardy" should also have a element that contains the string "Classics". The result is a boolean - true for a pass and false otherwise.
The next check demonstrates how a check written in XSLT is defined in the config. It checks exactly the same thing, but uses the extra expressive power of XSLT to provide a better message in the event of a failure:
<xsl:stylesheet xmlns:xsl="" version="2.0">

<!-- Checks that all books authored by Thomas Hardy are published by a publisher
     with "Classics" in their name.  (just to demonstrate a more complicated check) 

<xsl:output method="text" />

<xsl:template match="/">
<!-- Returns a sequnce of strings - 1 item per Hardy book.  If all pass a sequence
         of length 2 is produced ('true', 'true') 
<xsl:variable name="hardy" select="for $x in /booklist/books/item[author eq 'Thomas Hardy'] return if (not(contains($x/publisher, 'Classics'))) then concat(' Suspect publisher for Thomas Hardy: ', $x/publisher) else 'true'" />

<!-- Checks the sequence above for any values that aren't 'true' and returns
         a single 'true' if all pass (which is what xchecker requires of a successful
         check) or any error messages 
<xsl:sequence select="if ($hardy[. != 'true']) then $hardy[. != 'true'] else true()" />

Here the message "Suspect publisher for Thomas Hardy: Penguin" would be returned if the element contained "Penguin" instead of "Penguin Classics". The message can be whatever the test writer wants - as helpful or as useless as they see fit.
The final check is written in XPath again, and demonstrates how to output useful messages from XPath. Its reasonably verbose, so it could be moved into an XSLT or XQuery file - but its perfectly possible to write a complex XPath directly in the Check Config.
The xchecker processor takes the XML file and applies each of checks, if there are any error messages or non "true" results they are returned, otherwise the XML is considered correct.
4. Checking transforms
xchecker can be used to test XSLT transforms are correct by performing the transform and then applying the checks to the result. Here's an example Check Config that checks a transform:
<xchecker xmlns="">

<transform xml="xml/books.xml" xslt="xslt/books.xslt" result="result/output.html">
<check>/html/head/title = 'Books by publisher'</check>

books.xml is transformed by books.xslt and the result is written to output.html. The two XPaths are then applied to the result (which must be a well-formed XML file) - the first checks the title element and the second checks that a <meta> is present.
In this simple example both checks are written in XPath, but XML Schema, Relax NG, XSLT and XQuery can also be used.
5. Augmenting XML Schema with xchecker
The following XML consists of a father, mother, then some children in any order then pets:
The XML can be described by this XML Schema, which has an extra XPath 2.0 check which ensures all of children's names are different:
<xs:schema xmlns:xck="" xmlns:xs="">

<xck:check>count(/family/(son|daughter)) = count(distinct-values(/family/(son|daughter)))</xck:check>

<xs:element name="father" type="xs:string" />
<xs:element name="mother" type="xs:string" />
<xs:element name="son" type="xs:string" />
<xs:element name="daughter" type="xs:string" />
<xs:element name="pet" type="xs:string" />

<xs:group name="children">
<xs:element ref="son" />
<xs:element ref="daughter" />

<xs:element name="family">
<xs:element ref="father" />
<xs:element ref="mother" />
<xs:group ref="children" minOccurs="0" maxOccurs="10" />
<xs:element ref="pet" minOccurs="0" maxOccurs="10" />

As two <daughter>'s have the value "Jane" xchecker returns the message:

org.xml.sax.SAXException: XCHECKER check failed: false for: C:/xchecker/samples/xml/family.xml using: count(/family/(son|daughter)) = count(distinct-values(/family/(son|daughter)))
6. Running xchecker
To run the validation from Java create an XCheckerSchemaFactory, and then validate as normal:
SchemaFactory schemaFactory = new XCheckerSchemaFactory();
Schema schema = schemaFactory.newSchema(xsd);
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
To run xchecker from Java:
XChecker xchecker = new XChecker();
// or xchecker.processCheckSuite(checkSuite);
To run xchecker from the command line:
Use the -c switch when parsing a Check Config, using the -s when its a Check Suite or just pass in the XML and XSD when validating. For example:
java -cp xchecker.jar;lib/*.jar net.sf.xchecker.XChecker -c samples/CheckConfig.xml
java -cp xchecker.jar;lib/*.jar net.sf.xchecker.XChecker -s samples/CheckSuite.xml
java -cp xchecker.jar;lib/*.jar net.sf.xchecker.XChecker samples/xml/stock.xml samples/xsd/stock.xsd
7. An XML Schema for the Check Config
Below is the XML Schema that describes an xchecker Check Config file. It's been augmented with XPath 2.0 checks that ensure the paths used in the config point at parsable XML files.
<xs:schema xmlns:xck="" xmlns:xs="" xmlns="" targetNamespace="" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xck:check>//xck:xml/@src/doc-available(resolve-uri(., base-uri(/)))</xck:check>
<xck:check>//xck:transform/@xml/doc-available(resolve-uri(., base-uri(/)))</xck:check>
<xck:check>//xck:transform/@xslt/doc-available(resolve-uri(., base-uri(/)))</xck:check>

<xs:element name="xchecker" type="xchecker" />
<xs:element name="xml" type="xml-subject" />
<xs:element name="check" type="non-empty-string" />
<xs:element name="transform" type="transform-subject" />
<xs:element name="param" type="param" />

<xs:group name="subjects">
<xs:element ref="xml" />
<xs:element ref="transform" />

<xs:complexType name="xchecker">
<xs:group ref="subjects" maxOccurs="unbounded" />

<xs:complexType name="xml-subject">
<xs:element ref="check" maxOccurs="unbounded" />
<xs:attribute name="src" use="required" type="non-empty-string" />

<xs:complexType name="transform-subject">
<xs:element ref="param" minOccurs="0" maxOccurs="unbounded" />
<xs:element ref="check" maxOccurs="unbounded" />
<xs:attribute name="xml" use="required" type="non-empty-string" />
<xs:attribute name="xslt" use="required" type="non-empty-string" />
<xs:attribute name="result" use="required" type="non-empty-string" />

<xs:complexType name="param">
<xs:extension base="xs:string">
<xs:attribute name="name" type="non-empty-string" use="required" />

<xs:simpleType name="non-empty-string">
<xs:restriction base="xs:string">
<xs:minLength value="1" />