Skip to end of metadata
Go to start of metadata

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

  1. Transformer written in XSLT with namespaces.
  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 namespaces

In this example we build a very simple fragment transformer in XSLT. The point of this tutorial is purely to demonstrate how transforming xml with namespaces with Smooks. The use case for this example of transforming between two xml formats that use different namespace. This is a typical use case when one has a canonical data model.

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

<Order xmlns="" xmlns:xsi="">
        <date>Wed Nov 15 13:45:28 EST 2006</date>
        <state>South Dakota</state>
        <title>The 40-Year-Old Virgin</title>

And this is the expected result of our transformation:

<CanonicalOrderFormat *xmlns=""*>
        <title>The 40-Year-Old Virgin</title>

Notice that CanonicalOrderFormat has a different namepace and only contains a subset of the date from the Order xml instance. 

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. For more information on configuring XSLT resources, see XslContentDeliveryUnitCreator.

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

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

    <xsl:xsl applyOnElement="$document">
                    xmlns:xsl="" version="1.0"
                    exclude-result-prefixes="smk xhtml"

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

                <xsl:template match="/">
                    <CanonicalOrderFormat xmlns="">
                            <xsl:value-of select="./smk:Order/smk:header/smk:order-id"/>
                                <xsl:value-of select="./smk:Order/smk:order-item/smk:product-id"/>
                                <xsl:value-of select="./smk:Order/smk:order-item/smk:title"/>
                                <xsl:value-of select="./smk:Order/smk:order-item/smk:price"/>


The resource-config tells Smooks to apply the transformation that is declared "inline" in the  resource element on all <Order>. Notice that we are declaring namespace "" with prefix "smk". This namespace is then used in the select expressions to extract the values from the Order xml instance.

Also not the usage of exclude-result-prefixes which specifies that we wish to exclude the specified namespaces from the resulting xml. If this is not declared then then namespace for the prefix will added to the resulting xml, which is not something you want when you are trying to decouple. 

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