View RSS Feed

My Java Tips

XSLT processing in Java

Rate this Entry
by , 11-28-2011 at 07:25 PM (5049 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.

Source: http://xml.apache.org/xalan-j/

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:

http://xml.apache.org/xalan-j/downloads.html

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:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:output method="text" encoding="UTF-8">

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

</xsl:output></xsl:stylesheet
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 java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import org.xml.sax.SAXException;

public class SimpleXalan1 {

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

        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 del.icio.us Submit "XSLT processing in Java" to StumbleUpon Submit "XSLT processing in Java" to Google

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

Comments