Combining Groovy and XSLT for Data Transformation

In the blog post Beautiful Transformations with Groovy I described how easy it is to create data transformations with Groovy. But sometimes organisations invested massively in XSLT transformation and want to reuse their existing XSLT templates. Read on for an an example that shows how to do that.

Assume we want to transform the following XML file to HTML:


  
    Germany
    Fast and nice
  
  
    Spain
    Large and handy
  
  
    Italy
    Small and cheap
  

Lets further assume the result should look like this:

Does it make sense? I don’t know, but that’s not really important. 😉

We have the following XSLT template to perform the transformation:

All you need is a Groovy script like the one below to transform the xml file to html using the given xslt.

// Load xslt
def xslt= new File("template.xsl").getText()

// Create transformer
def transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt)))

// Load xml
def xml= new File("cars.xml").getText()

// Set output file
def html = new FileOutputStream("output.html")

// Perform transformation
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(html))

This is self-explanatory, isn’t it?
As XSLT is somewhat limited when it comes to more complex transformations, it can be extended by custom processors which can we implemented in Java or Groovy. A custom processor in Groovy can be implemented like this:

public class AgeProcessor{
    public def process(ExpressionContext context,int n){
        return "Age: " + (2012 - n) + " years";
    }
}

The processor is hooked up to the XSLT using the expressions in line 3 and 28 of the above XSLT file.
The examples above show how to reuse existing XSLT in Groovy. Are you interested to see the same same transformation in pure Groovy? (sorry, I could not resist ;-))
Here is the code:

// Load xml
def cars = new XmlSlurper().parse(new File("cars.xml"))

// Set output file
def writer = new FileWriter("output.html")

// Perform transformation
def builder = new MarkupBuilder (writer);
builder.html(xmlns:"http://www.w3.org/1999/xhtml") {
    head {
        title "Cars collection"
    }
    body {
        h1("Cars")
        ul(){
            cars.car.each{car ->
                li(car.@name.toString() + "," + car.country + "," + car.description + ", Age: " + (2012 - car.@year.toInteger()) + " years")
            }
        }
    }
 }

It is shorter and self-contained. It is also more intuitive and therefore easier to maintain. But if you have the requirement to support XSLT in Groovy you now know how to do that.

Beautiful Transformations with Groovy

Data transformations are the daily business in ETL and ESB scenarios. If you have a service- or business process boundary it is very likely that data has to be transformed between different representation.
Typical integration scenarios have to deal with a huge amount of different formats (flat file, xml, csv, json, even binary). To make things even worse they tend to change from time to time. Therefore it is important to have tools that are flexible and easy to understand. I found Groovy to be very handy for the transformation job. Let’s have a look at a typical example. Assume we have a flat file in the following format:

Id Product     Amount
 1 Cheese      15
 2 Sausage     2
 3 Bread       3
 4 Butter      4

What we want is an xml representation like this:


  
    
    Cheese
    15
  
  
    
    Sausage
    2
  
  
    
    Bread
    3
  
  
    
    Butter
    4
  

The groovy script below does the job. The great thing is not only that is works but also that its easy to develop and maintain. A developer can see the source and target structure at the same time.

def reader = new StringReader(flatfile);
def writer = new StringWriter();
def builder = new groovy.xml.MarkupBuilder(writer)

builder.order(){
    reader.eachLine() {
        def line = it
        item(){  
            id(id:line.getAt(0..1).trim())
            name(line.getAt(3..14).trim())
            amount(line.substring(15).trim())
        }
    }
}

println writer.toString();

Thanks to closures an the MarkupBuilder class the transformation can be written in a fluent and intuitive way. Typical other transformations are as simple as that, making Groovy a first choice for data transformations.

Increase Efficiency using Groovy, DSL and SOA

Recently SpringSource published a case study of a project which I supported as system architect and lead developer from concept to production.
The case study shows how organisations can greatly increase efficiency by the use of modern technology approaches such as Groovy, SOA and Domain Specific Languages (DSL).
You can read the case study here.

Dehydrating long running Groovy DSLs

In my recent article about Domain Specific Languages (DSLs) published in the 12/2009 issue of Javamagazin I described how to implement DSLs using Groovy.
In the article I mentioned that dehydration (state persistence) could be added to the DSL runtime if needed to support long running processes.
I’ve created a Groovy script dehydration.groovy to illustrate how this could be achieved.
Basically the Groovy script is stopped by terminating the executing thread and the state is pulled out.
Later on the state is injected and the remaining part of the script is executed.
This approach can be used to process any kind of long running scripts, especially if they have to wait for external events.