View RSS Feed

My Java Tips

XSLT processing in Java

Rate this Entry
by , 11-28-2011 at 06:25 PM (6385 Views)
XSLT (Extensible Stylesheet Language Transformations) is used to transform XML files into other formats like HTML format. There are many XSLT processors (libraries) available to be used in Java for XSLT transformation. These libraries can be used from a Java application like JSP/Servlet to read a XML file and to transform it into a HTML.

An XSLT processor takes two inputs: an XML file and an XSLT stylesheet.

For this post, I have chosen Xalan-Java library for the transformation.

Xalan-Java is an XSLT processor for transforming XML documents into HTML, text, or other XML document types. It implements XSL Transformations (XSLT) Version 1.0 and XML Path Language (XPath) Version 1.0 and can be used from the command line, in an applet or a servlet, or as a module in other program.


To use Xalan-Java, we have to include its JAR file in the class path so we can invoke the appropriate methods. You may download the required JARs from:

Now lets take an example. We have following XML file:
Java Code:
<!--?xml version="1.0" encoding="UTF-8"?-->
<message>Yep, it worked!</message>

For the XML file given in the first part of this post, we have the following XSLT stylesheet.
Java Code:
<!--?xml version="1.0" encoding="UTF-8"?-->
  <xsl:output method="text" encoding="UTF-8">

  <xsl:template match="/">
    <xsl:value-of select="message">

Now we want to transform the XML file using XSLT stylesheet and want to print it on the console. We will use xalan engine for this.
Java Code:
import org.xml.sax.SAXException;

public class SimpleXalan1 {

    public static void main(String[] args)
            throws MalformedURLException, SAXException {
        if (args.length != 2) {
            System.err.println("  java " + SimpleXalan1.class.getName(  )
                    + " xmlFileName xsltFileName");

        String xmlFileName = args[0];
        String xsltFileName = args[1];

        String xmlSystemId = new File(xmlFileName).toURL().toExternalForm(  );
        String xsltSystemId = new File(xsltFileName).toURL().toExternalForm(  );

        org.apache.xalan.xslt.XSLTProcessor processor =
                org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor(  );

        org.apache.xalan.xslt.XSLTInputSource xmlInputSource =
                new org.apache.xalan.xslt.XSLTInputSource(xmlSystemId);

        org.apache.xalan.xslt.XSLTInputSource xsltInputSource =
                new org.apache.xalan.xslt.XSLTInputSource(xsltSystemId);

        org.apache.xalan.xslt.XSLTResultTarget resultTree =
                new org.apache.xalan.xslt.XSLTResultTarget(System.out);

        processor.process(xmlInputSource, xsltInputSource, resultTree);
Happy codding!

Submit "XSLT processing in Java" to Facebook Submit "XSLT processing in Java" to Digg Submit "XSLT processing in Java" to Submit "XSLT processing in Java" to StumbleUpon Submit "XSLT processing in Java" to Google

Tags: html, xalan-java, xml, xslt Add / Edit Tags