jOOX and XSLT. An XML love story, continued

jOOX - a jQuery port to Java The somewhat functional way of thinking involved with jOOX’s XML manipulation cries for an additional API enhancement simply supporting XSLT. XSL transformation has become quite a standard way of transforming large amounts of XML into other structures, where normal DOM manipulation (or jOOX manipulation) becomes too tedious. Let’s have a look at how things are done in standard Java

Example input:

  <book id="1"/>
  <book id="2"/>

Example XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"

    <!-- Match all books and increment their IDs -->
    <xsl:template match="book">
        <book id="{@id + 1}">

    <!-- Identity-transform all the other elements and attributes -->
    <xsl:template match="@*|*">
            <xsl:apply-templates select="*|@*"/>

Verboseness of XSL transformation in Java

The standard way of doing XSL transformation in Java is pretty verbose – as just about anything XML-related in standard Java. See an example of how to apply the above transformation:

Source source = new StreamSource(new File("increment.xsl"));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(source);
DOMResult result = new DOMResult();
transformer.transform(new DOMSource(document), result);

Node output = result.getNode();

Drastically decrease verbosity with jOOX

With jOOX, you can write exactly the same in much less code:

Apply transformation:
// Applies transformation to the document element:

// Applies transformation to every book element:

The result in both cases is:

  <book id="2"/>
  <book id="3"/>

Tags: , , , , , ,

3 responses to “jOOX and XSLT. An XML love story, continued”

  1. uklance says :

    I have used XSLT many times in the past and hate it for it’s verboseness and how hard it makes it to do simple things such as calling macros.

    Take freemarker’s XML support for a spin and I’m sure you’ll find it to be much simpler, easier to read, less verbose and more powerful than XSLT. It has great XML support including XPath (

    • lukaseder says :

      That looks quite interesting! Does Freemarker implement javax.xml.transform.Transformer? Then, Freemarker could be used with jOOX in the way I described above

      • uklance says :

        javax.xml.transform.Transformer expects XML as the input. Freemarker is a generic templating language that can take any object as input. It just happens to have great support for XML inputs. I’m not sure that javax.xml.transform.Transformer is the right fit.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 3,805 other followers

%d bloggers like this: