This is an AMP version of the article, its original content can be found here.

XSL Transformation in Java: An Easy Way

XSL transformation (XSLT) is a powerful mechanism for converting one XML document into another. However, in Java, XML manipulations are rather verbose and complex. Even for a simple XSL transformation, you have to write a few dozen lines of code—and maybe even more than that if proper exception handling and logging is needed. jcabi-xml is a small open source library that makes life much easier by enabling XML parsing and XPath traversing with a few simple methods. Let's see how this library helps in XSL transformations.

First, take a look at a practical example—rultor.com—a hosted DevOps assistant that automates release, merge, and deploy operations. Rultor keeps each conversation session with an end user (a.k.a. "talk") in a DynamoDB record. There are multiple situations to handle in each talk; that's why using multiple columns of a record is not really feasible. Instead, we're keeping only a few parameters of each talk in record columns (like ID and name) and putting all the rest in a single XML column.

This is approximately how our DynamoDB table looks:

Once a user posts @rultor status into a GitHub ticket, Rultor has to answer with a full status report about the current talk. In order to create such a text answer (a regular user would not appreciate an XML response), we have to fetch that xml column from the necessary DynamoDB record and convert it to plain English text.

Here is how we're doing that with the help of jcabi-xml and its class, XSLDocument.

That's it. Now let's see what's there in that status.xsl file (this is just a skeleton of it; the full version is here):

It is good practice to create XSL documents only once per application run. We have a static utility method XSLDocument.make() for this:

Pay attention to the fact we're using XSLT 2.0. Built-in Java implementation of XSLT doesn't support version 2.0, and in order to make it work, we're using these two Maven Saxon dependencies:

All you need to do to start using jcabi-xml for XSL transformations is add this dependency to your pom.xml:

If you have any problems or suggestions, don't hesitate to submit an issue to the GitHub issue tracker.