Skip to end of metadata
Go to start of metadata

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

  1. A very basic Fragment Transformer written in XSLT.
  2. The Smooks configuration file.
  3. Executing the Smooks Transformation.

SVN - Download - Other Tutorials

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

The Fragment Transformer

In this example we build a very simple (silly) fragment transformer in XSLT. The applied transform is exactly the same as that carried out in the "java-basic" tutorial. Sure, it's totally trivial. The point of this tutorial is purely to demonstrate how to hook in a fragment transformer into Smooks. More realistic usecases can be seen in some of the other tutorials.

So here's the XSLT we use in this tutorial:

<!-- /example/BasicJavaTransformer.xsl -->
<xsl:stylesheet xmlns:xsl=""

    <xsl:output method="xml" encoding="UTF-8" />

    <xsl:template match="b">


It simply generates an "<xxx></xxx>" fragment for the currently matching "b" context element.

The Smooks Configuration

In order to apply this transformer to a message fragment, a Smooks Configuration needs to be created. This configuration will target the transformer at a particular message fragment.

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

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

    <xsl:xsl applyOnElement="c/b">


As with the java-basic tutorial, the resource-config tells Smooks to apply the "/example/BasicJavaTransformer.xsl" resource on all <b> elements that are enclosed by a parent <c> element.

So, taking the sample message supplied with this example:


Smooks produces the following output(same as with the java-basic tutorial):


Executing The Transformation

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

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

// Filter the input message to the outputWriter...
smooks.filter(new StreamSource(messageInStream), new StreamResult(messageOutStream));

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

See the example/ in the example source.

  • No labels