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 6 Next »

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:

<Order>
    <header>
        <order-id>1</order-id>
        <status-code>0</status-code>
        <net-amount>59.97</net-amount>
        <total-amount>64.92</total-amount>
        <tax>4.95</tax>
        <date>Wed Nov 15 13:45:28 EST 2006</date>
    </header>
    <customer-details>
        <username>user1</username>
        <name>
            <firstname>Harry</firstname>
            <lastname>Fletcher</lastname>
        </name>
        <state>South Dakota</state>
    </customer-details>
    <order-item>
        <position>1</position>
        <quantity>1</quantity>
        <product-id>364</product-id>
        <title>The 40-Year-Old Virgin</title>
        <price>29.98</price>
    </order-item>
    <order-item>
        <position>2</position>
        <quantity>1</quantity>
        <product-id>299</product-id>
        <title>Pulp Fiction</title>
        <price>29.99</price>
    </order-item>
</Order>

And this is the expected result of our transformation:

<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"/>
    <OrderLines>
        <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>
    </OrderLines>
</Order>

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"):

<?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">

    <!--
    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">
	    <resource>/example/DateFormatter.groovy</resource>
        <param name="input-format">EEE MMM dd HH:mm:ss z yyyy</param>
        <param name="output-format">
            time=HH:mm
            day=dd
            month=MM
            year=yy
        </param>
    </resource-config>

    <!--
    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">
        <xsl:template>/example/order.xsl</xsl:template>
    </xsl:xsl>

</smooks-resource-list>

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");

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

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

See the example/Main.java in the example source.

  • No labels