Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Groovy's XML capabilities are powerful enough that you may not need to consider other alternatives such as XSLT. If you wish to use XSLT though, it isn't hard to do. Here we just added xalan and serializer jars from the Apache Xalan-J project to our CLASSPATH. Depending on your JVM version and other available libraries, you may need slightly different jars (or none at all). Once our path is set up, we can run the following script to convert an XML file into XHTML:

Code Block
// require(url='', jar='serializer.jar')
// require(url='', jar='xalan_270.jar')
import javax.xml.transform.TransformerFactory

def input = '''
<?xml version="1.0" ?>
    <person username="JS1">
    <person username="ND1">

def xslt = '''
<xsl:stylesheet xmlns:xsl="" version="1.0">
    <xsl:template match="/persons">
        <html xmlns="">
            <title>Testing XML Example</title>
            <xsl:apply-templates select="person">
                <xsl:sort select="family_name" />
    <xsl:template match="person">
            <xsl:value-of select="family_name"/>,
            <xsl:value-of select="name"/>

def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(new StringReader(xslt)))
transformer.transform(new StreamSource(new StringReader(input)), new StreamResult(System.out))

Here is the output (pretty-printed):

Code Block
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="">
<head><title>Testing XML Example</title></head>
    <li>Davolio, Nancy</li>
    <li>Smith, John</li>

Depending on your environment (JVM, XSLT processor, operating system) you might be able to make use of XSLT directly from the command line instead of calling it from Java or Groovy. You can also call it from Ant using the xslt task.