Transform XML using XSLT in Java

This article explains how to transform XML documents using XSLT.
XSLT (eXtensible Stylesheet Language Transformations) is a language for transforming XML documents into HTML, XML, or other types of documents. Formatting rules to transform the input XML data is specified in a XML stylesheet (XSL). Inside the XSL typically XML Path language (XPath) is used to identify different parts of the XML document.
Xalan-Java (http://xml.apache.org/xalan-j/) is an XSLT processor for transforming XML documents. Xalan-Java fully implements W3C specification of XSLT version 1.0 and XPath version 1.0.

Simple Java program to transform XML into HTML

In this example we are interested to transform an XML document into a HTML table for presentation.

This is input XML data file.

<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applicationswith XML.</description></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies,an evil sorceress, and her own childhood to become queenof the world.</description></book><book id="bk103"><author>Corets, Eva</author><title>Maeve Ascendant</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-11-17</publish_date><description>After the collapse of a nanotechnologysociety in England, the young survivors lay thefoundation for a new society.</description></book></catalog>
This is input XSL file.
<?xml version="1.0" encoding="UTF-8"?><html xsl_version="1.0" ><body><table border="1"><tr><th>Title</th><th>Author</th><th>Description</th><th>Genre</th><th>Price</th></tr><xsl:for-each select="catalog/book"><tr><td><xsl:value-of select="title"/></td><td><xsl:value-of select="author"/></td><td><xsl:value-of select="description"/></td><td><xsl:value-of select="genre"/></td><td><xsl:value-of select="price"/></td></tr></xsl:for-each></table></body></html>
Here is the Java program to perform the transformation. Note that we use a system property to specify that we are interested in using Xalan XSLT processor.
package com.sourcetricks.transform;import java.io.FileInputStream;import java.io.FileOutputStream;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;public class TransformXml {public static void main(String[] args) {// Input xml data fileString xmlInput = "resources/input.xml";// Input xsl (stylesheet) fileString xslInput = "resources/input.xsl";// Output html fileString htmlOutput = "/tmp/output.html";// Set the property to use xalan processorSystem.setProperty("javax.xml.transform.TransformerFactory","org.apache.xalan.processor.TransformerFactoryImpl");// try with resourcestry ( FileOutputStream os = new FileOutputStream(htmlOutput) ){FileInputStream xml = new FileInputStream(xmlInput);FileInputStream xsl = new FileInputStream(xslInput);// Instantiate a transformer factoryTransformerFactory tFactory = TransformerFactory.newInstance();// Use the TransformerFactory to process the stylesheet source and produce a TransformerStreamSource styleSource = new StreamSource(xsl);Transformer transformer = tFactory.newTransformer(styleSource);// Use the transformer and perform the transformationStreamSource xmlSource = new StreamSource(xml);StreamResult result = new StreamResult(os);transformer.transform(xmlSource, result);}catch (Exception e){e.printStackTrace();}}}
This is the output of the program.

Try the SmallWebTools – Free Online XSL Transformer