Obix provides the DOMTransform class as a wrapper to the core Java API for XSL transformations; its aim is to reduce the aggregate lines of code that are required for achieving XSL transforms. In this section, we provide a simple example application which shows how to use this wrapper for achieving an XML transformation with a simple stylesheet.
The example application can be downloaded from our subversion repository, which is accessible via this link. Please note that you will need an obix labs account in order to access the repository as we discourage anonymous access. When setup in the Eclipse IDE, the sample application is laid out as follows:

The example application consists of a single class and entry point: TransformExample. It also consists of an XML file and a stylesheet which is used to transform it. In order to understand the application output, let us quickly go through both the sample XML document and the given stylesheet. The XML file, world-greetings.xml, is shown in the following listing. As its name implies, its intent is to capture the greeting/salutation used in different countries. For the sake of brevity, we have restricted it to just two countries: The United Kingdom and France:
<world-greetings>
<country name="The United Kingdom">
<greeting>Hello</greeting>
</country>
<country name="France">
<greeting>Bonjour</greeting>
</country>
</world-greetings>
The stylesheet which we want to apply to this document is as follows:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/world-greetings/country/greeting">
The greeting in <xsl:value-of select="../@name"/> is <xsl:value-of select="text()"/>
</xsl:template>
</xsl:stylesheet>
The intent of the stylesheet is to print out a phrase of the form: "The greeting in x is y", substituting x and y for the country names and greeting respectively. These values are in turn taken from the @name attribute and the <greeting> child of the <country> node respectively.
To see the output of this transform, execute the class TransformExample within your IDE, and it should produce the following output:
The greeting in The United Kingdom is Hello
The greeting in France is Bonjour
To see how the actual transform is achieved in Java, let us look at the main method of the TransformExample class, which is shown in the following listing.
public static void main(String[] args) throws ObixException
{
Document xml = readXML();
byte[] xsl = readXSL();
DOMTransform transform = new DOMTransform(xsl);
byte[] transformResult = transform.transform(xml, null);
System.out.println(new String(transformResult));
}
If we discount the lines to read the xsl stylesheet and the input xml document, and also the line to print out the result, we can see that there are only two significant lines of code. One of these creates the DOMTransform instance with the XSL stylesheet and the other initiates the actual transform. This is significantly less code than would otherwise have been written for a Java XML/XSLT transform.
We will not delve into the details of the methods readXML() and readXSL(), as the way in which the XML documents and stylesheets are supplied to the DOMTransform are not strictly relevant. We can however state that the method both use the classpath and XML utilities from the commons library to read the XML document and stylesheet from the application's classpath. As such, it is important to ensure that your IDE does not filter these resources out of the compile output.