Versions Compared


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

The primary goals of this example are to introduce you to the following:

  1. Transformer that uses xslt and groovy.
  2. The Smooks configuration file.
  3. Executing the Smooks Transformation.

SVN - Download - Other Tutorials

Other Relevant Info:

To Build: "mvn clean install"
To Run: "mvn exec:java"


Transforming xml with xslt and groovy

This example illustrates how Smooks can be used to combine Groovy scripting with XSLT to perform an Order message transform. In this example, Smooks applies a piece of Groovy script to a message "date" fragment.  This script parses the date, re-adding it to the message as a set of nodes containing the date's consituent parts.  The main transformation is then carried out on the whole document ("$document") using XSLT.

What this illustrates is how easy it is to combine the powers of different technologies under a single Smooks transform.  We use Groovy to pre-process a message fragment that is very difficult to process using XSLT (i.e. a date string).  We then use XSLT to do what it's good at i.e. templating.

Of course you can get similar functionality to this using XSLT Extensions. However, the difference is that this approach allows you to maintain portability across XSLT Processors.  Note below how you can switch XSLT Processors.

So here's the source xml that is to be transformed:

No Format
        <date>Wed Nov 15 13:45:28 EST 2006</date>
        <state>South Dakota</state>
        <title>The 40-Year-Old Virgin</title>
        <title>Pulp Fiction</title>

And this is the expected result of our transformation:

No Format
<Order date="11-15-06" netAmount="59.97" orderId="1" statusCode="0" tax="4.95" totalAmount="64.92">
    <Customer firstName="Harry" lastName="Fletcher" state="South Dakota" userName="user1"/>
        <order-item price="29.98" product-id="364" quantity="1">The 40-Year-Old Virgin</order-item>
        <order-item price="29.99" product-id="299" quantity="1">Pulp Fiction</order-item>


The Smooks Configuration

In order to apply this transformer to a message fragment, a Smooks Configurationneeds to be created. This configuration will target the transformer at a particular message fragment. For more information on configuring XSLT resources, see XslContentDeliveryUnitCreator.

Here's the configuration ("smooks-config.xml"):

No Format
<?xml version="1.0"?>
<smooks-resource-list xmlns=""

    Break out the <date> field into separate "time", "day", "month" and "year" fields using Groovy.
    This makes the complex date field value consumable by something like XSLT.
    <resource-config selector="header date">
        <param name="input-format">EEE MMM dd HH:mm:ss z yyyy</param>
        <param name="output-format">

    Transform the document (as a whole) using XSLT.  The complex date field has already been preprocessed into
    separate fields using Groovy (see above config) - XSLT can handle it easily now :-)
    <xsl:xsl applyOnElement="$document">



Executing The Transformation

Again, it's exactly the same as with the java-basic tutorial:

No Format
// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");

smooks.filter(new StreamSource(inStream), new StreamResult(outStream));

Of course, you'd typically cache the Smooks instance.

See the example/ in the example source.