There’s a good variety of options available for transforming XML. In addition to the standards (XSLT and XQuery), Groovy has excellent XML manipulation support with MarkupBuilders and XMLSlurpers.

I wanted to know how XSLT and Groovy compare in terms of performance, but couldn’t find anything online. So, I decided to measure it myself.

DISCLAIMER: I tried as much as I could to make the comparison fair. For instance, I kept both the XML and the transform logic as simple as possible. That said, exact steps each transform follows aren’t identical and that may have impacted the results. Moreover, I used standard java libraries based on Xalan (javax.xml.transform.*) to execute the XSLT code. It may be possible to get better results with Saxon.

My results show that Groovy was consistently faster, about 44% on average. The details of my tests are shown below.

The Test

Here’s the XML I wanted to transform:


I wrote two transforms, in XSLT and in Groovy, to generate a simple HTML page. Here’s the XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="">
    <xsl:template match="/">
                <title>list of items</title>
                    <xsl:apply-templates select="items/item" />

    <xsl:template match="item">
            <xsl:value-of select="name"/>

And here’s the Groovy:

def inXml = new XmlSlurper().parse(xmlFile)
def writer = new StringWriter()
def outXml = new MarkupBuilder(writer)

outXml.html {
    head {
        title {'list of items'}
    body {
        h1 {'items'}
        ul {
            inXml.item.each {

I then ran both transforms and timed their execution results:

xsltTransformer = factory.newTransformer(new StreamSource(new FileReader(xsltPath)))
xml = new FileReader(xmlPath)
groovyTransformer = new GroovyXmlTransformer()

xsltStart = new Date()
xsltTransformer.transform(new StreamSource(xml), new StreamResult(System.out))
xsltStop = new Date()
xslElapsed =  TimeCategory.minus(xsltStop, xsltStart)

groovyStart = new Date()
groovyTransformer.transform(new FileReader(xmlPath))
groovyStop = new Date()
groovyElapsed = TimeCategory.minus(groovyStop, groovyStart)

The Results

I ran three sets of comparisons (100 times each, transforming an XML document with a 1,000 items). The results are shown below:

run 1

run 2

run 3

You may also like:

Did you love / hate / were unmoved by this post?
Then show your support / disgust / indifference by following me on Twitter!

Comments are closed.