Re: [xsl] XSLT Unit testing

Subject: Re: [xsl] XSLT Unit testing
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Thu, 18 Aug 2011 10:35:35 +0100
> My experience does suggest that it's not always going to be a worthwhile
> investment of effort, but that when it is worthwhile it can be very
> worthwhile.

That kind of sums up how I feel about writing tests in general, most
of the time its a chore but once they are in place you are really glad
you did it :)

For what its worth, I've not used xslt specific test frameworks,
instead using standard junt tests that run the transform and then use
xpaths on the result (well, xquery).  Because its just a normal junit
test, it slots straight in with existing build process.  You can test
individual templates and functions by using a custom xslt that imports
the main transform and then just calls the function or applies
templates to a variable.  You can be as simple or a complex as you
like - for example you could write a single transform that calls a
function 10 different ways producing some output xml with the 10
results, then in the junit test pull out those values and compare

The basic code is below, most of it is boilerplate to run the
transform  (using Saxon) and make the actual test methods small. To
use it, just copy and paste it, edit the XML and XSLT paths, and then
start adding tests.  The tests themselves can be as simple as:

    public void testTheTitle() throws Exception {
        assertEquals("Some value for the title",

Here's the code:

import static org.junit.Assert.*;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.InputSource;
import javax.xml.transform.sax.SAXSource;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.XQueryCompiler;
import net.sf.saxon.s9api.XQueryEvaluator;
import net.sf.saxon.s9api.XdmDestination;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XsltTransformer;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestingXSLTUsingXQuery {

    private static final String XSLT = "file:///c:/path/to/test.xslt";
    private static final String XML = "file:///c:/path/to/test.xml";

    private static XQueryCompiler xqueryCompiler;
    private static XdmNode doc;

    public static void setUp() {
        try {
            Processor processor = new Processor(false);
            xqueryCompiler = processor.newXQueryCompiler();

            XsltTransformer transformer =
processor.newXsltCompiler().compile(new SAXSource(new

            XdmDestination destination = new XdmDestination();
            transformer.setSource(new SAXSource(new

            doc = destination.getXdmNode();
        } catch (Exception ex) {
null, ex);

    private String runXQuery(String xquery) throws Exception {
        XQueryEvaluator xqEval = xqueryCompiler.compile(xquery).load();

        return xqEval.evaluate().toString();

    public void testTheFirstFooStringValue() throws Exception {

        String xquery = "//foo[1]/string(.)";

        String expected = "123";

        assertEquals(expected, runXQuery(xquery));

    public void testFooContent() throws Exception {

        String xquery = "let $expected :=
<root><foo>123</foo><foo>456</foo></root> return deep-equal(/root,


Andrew Welch

Current Thread