Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 15 Current »

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="http://www.w3.org/1999/XSL/Transform"
				version="1.0">

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

    <xsl:template match="b">
        <xxx></xxx>
    </xsl:template>

</xsl:stylesheet>

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="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:xsl="http://www.milyn.org/xsd/smooks/xsl-1.1.xsd">

    <xsl:xsl applyOnElement="c/b">
        <xsl:template>/example/BasicXslTransform.xsl</xsl:template>
    </xsl:xsl>

</smooks-resource-list>

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:

<a>
    <b>
        <c>
            <b></b>
        </c>
    </b>
</a>

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

<a>
    <b>
        <c>
            <xxx></xxx>
        </c>
    </b>
</a>

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/Main.java in the example source.

  • No labels