Home > Articles > Programming > Java

Transforming XML with XSLT

  • Print
  • + Share This
Xalan is the standard transformer for converting XML data into HTML. In this article, Larry Brown summarizes installation and setup of the Apache Xalan transformer and presents a simple Swing text editor to transform XML documents. Also, a JavaServer Page custom tag is presented to transform XML documents into different HTML documents based on the client's browser type.
Larry Brown is a co-author (with Marty Hall) of Core Web Programming (Prentice Hall PTR, 2001, ISBN 0-13-089793-0). This article is adapted from the book.
Like this article? We recommend

Like this article? We recommend

XSLT is a language for transforming XML documents into HTML, XML, or other types of documents. When performing a transformation, an XSLT engine converts the XML document according to formatting rules and XPath addresses specified in an XML style sheet (XSL). The XPath information identifies the different parts of the XML document for processing, and the style sheet information identifies the layout of the output.

The benefit of XSLT is that you can define multiple style sheets for transforming a single XML document. For example, a database could return a query in an XML format, and, depending on the client protocol, HTTP or WAP, a servlet could use different style sheets to convert the data into HTML or WML, respectively. As another example of an XSLT application, consider an e-commerce business order; the order could be sent to the supplier in an XML format and then processed by the recipient with XSLT, using different XSL documents to convert the original order into separate billing and shipping documents.

Specifications for XSLT, XSL, and XPath technologies are maintained by the WWW Consortium (W3C) at the following locations:

Upcoming specifications are summarized at http://www.w3.org/Style/XSL/. In addition, GoXML sponsors an excellent XSLT resource site.

To download a zip containing the source files for this article, click here.

Installation and Setup

XSLT is not a standard part of either Java 2 Standard Edition or the servlet and JSP APIs. So, your first step is to download the appropriate classes and configure them for use in your programs. Here is a summary of what's required:

  1. Download an XSLT-compliant transformer. The transformer provides the Java classes that follow the XSLT 1.0 specification as specified by the W3C. You can obtain a list of XSLT parsers at http://www.w3.org/Style/XSL/ or http://www.xslt.com/xslt_tools_engines.htm. We use the Apache Xalan-Java (Xalan-J) transformer in Core Web Programming.

  2. Set your CLASSPATH to include the DOM and SAX classes. XSLT builds on DOM and SAX for handling the document processing. In the case of Apache Xalan-J, you need to include xerces.jar in the CLASSPATH. For example, for desktop application on Windows, you would do this:

    set CLASSPATH=xerces_install_dir\xerces.jar;

    On Unix/Linux and the C shell, you would do this:

    setenv CLASSPATH xerces_install_dir/xerces.jar:

    Note that xerces.jar is included in the Xalan-J installation directory. Tomcat 4.0 already uses Xerces-J for parsing XML documents, so if using DOM or SAX in your servlets and JavaServer Pages, xerces.jar is already installed and located in the TOMCAT_HOME\common\lib directory.

  3. Set your CLASSPATH to include the XSLT classes. With Xalan, these classes are in xalan_install_dir\xalan.jar. Similarly, for desktop applications on Windows, you would do this:

    set CLASSPATH=xalan_install_dir\xalan.jar;

    On Unix/Linux and the C shell, you would do this:

    setenv CLASSPATH xalan_install_dir/xalan.jar:

    If you wanted to use XSLT from servlets and JSP, you would copy the appropriate DOM, SAX, and XSLT JAR files to the server's lib directory (if supported); unpack the JAR files (using jar -xvf) into the server's classes directory; or explicitly change the server's CLASSPATH, usually by modifying the server's startup script. If configuring Tomcat 4.0 for XSLT, place xalan.jar in the TOMCAT_HOME\common\lib directory.

  4. Bookmark the XSL 1.0 and XPath 1.0 specifications. The official documentation for these two specifications can be found at http://www.w3.org/Style/XSL/.

  5. Bookmark the XSLT specification. The official XSLT specification can be found at http://www.w3.org/TR/xslt.html. The XSLT specification is implemented in Apache Xalan through the Transformation API for XML (TrAX). The complete TrAX API comes with Xalan-J in Javadoc format and is also available online at http://xml.apache.org/xalan-j/apidocs/.

  • + Share This
  • 🔖 Save To Your Account