IDE

Smooks v1.2

Smooks 1.0

Example - xslt-basic

The primary goal of this example is to illustrate how XSL Transforms can be applied to message fragments using Smooks.  The source message in this example is XML, but it could easily be EDI, CSV, JSON etc depending on the configured Reader.


 
SVN - Download

 
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 transformation is totally trivial. The point of the example is purely to demonstrate how to hook in a fragment transformer into Smooks.

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>

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:

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

Executing The Transformation

// 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.