Saturday, January 25, 2014
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 ( 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"?>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <description>An in-depth look at creating applications 
      with XML.</description>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
This is input XSL file.
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="">
  <table border="1">
   <xsl:for-each select="catalog/book">
       <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>
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 javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

public class TransformXml {

 public static void main(String[] args) {
  // Input xml data file
  String xmlInput = "resources/input.xml";
  // Input xsl (stylesheet) file
  String xslInput = "resources/input.xsl";
  // Output html file
  String htmlOutput = "/tmp/output.html";
  // Set the property to use xalan processor
  // try with resources
  try ( FileOutputStream os = new FileOutputStream(htmlOutput) )
   FileInputStream xml = new FileInputStream(xmlInput);
   FileInputStream xsl = new FileInputStream(xslInput);
   // Instantiate a transformer factory
   TransformerFactory tFactory = TransformerFactory.newInstance();
   // Use the TransformerFactory to process the stylesheet source and produce a Transformer 
   StreamSource styleSource = new StreamSource(xsl);
   Transformer transformer = tFactory.newTransformer(styleSource);
   // Use the transformer and perform the transformation
   StreamSource xmlSource = new StreamSource(xml);
   StreamResult result = new StreamResult(os);
   transformer.transform(xmlSource, result);
  catch (Exception e)
This is the output of the program.

Try the SmallWebTools - Free Online XSL Transformer


Post a Comment

Contact Form


Email *

Message *

Back to Top