O'Reilly logo

Java and XSLT by Eric M. Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Started

The best way to get started with new technologies is to experiment. For example, if you do not know XSLT, you should experiment with plenty of stylesheets as you work through the next two chapters. Aside from trying out the examples that appear in this book, you may want to invent a simple XML data file that represents something of interest to you, such as your personal music collection or family tree. Using XSLT stylesheets, try to create web pages that show your data in many different formats.

Once the basics of XSLT are out of the way, servlets will be your next big challenge. Although the servlet API is not particularly difficult to learn, configuration and deployment issues can make it difficult to debug and test your applications. The best advice is to start small, writing a very basic application that proves your environment is configured correctly before moving on to more sophisticated examples. Apache’s Tomcat is probably the best servlet container for beginners because it is free, easy to configure, and is the official reference implementation for Sun’s servlet API. A servlet container is the server that runs servlets. Chapter 6 covers the essentials of the servlet API, but for all the details you will want to pick up a copy of Java Servlet Programming by Jason Hunter (O’Reilly). You definitely want to get the second edition because it covers the dramatic changes that were introduced in Version 2.2 of the servlet API.

Java XSLT Processor Choices

Although this book uses primarily Sun’s JAXP and Apache’s Xalan, many other XSLT processors are available. Processors based on other languages may offer much higher performance when invoked from the command line, primarily because they do not incur the overhead of a Java Virtual Machine (JVM) at application startup time. When using XSLT from a servlet, however, the JVM is already running, so startup time is no longer an issue. Pure Java processors are great for servlets because of the ease with which they can be embedded into the web application. Simply adding a JAR file to the CLASSPATH is generally all that must be done.

Putting an up-to-date list of XSLT processors into a book is futile because the market is maturing too fast. Some of the currently popular Java-based processors are listed here, but a quick web search for “XSLT Processors” would be prudent before you decide to standardize on a particular tool, as new processors are constantly appearing. We will see how to use Xalan in the next chapter; a few other choices are listed here.


XT was one of the earliest XSLT processors, written by James Clark. If you read the XSLT specification, you may recognize him as the editor of the XSLT specification. As the XSLT specification evolved, XT followed a parallel path of evolution, making it a leader in terms of standards compliance. At the time of this writing, however, XT had not been updated as recently as some of the other Java- based processors. Version 19991105 of XT implements the W3C’s proposed-recommendation (PR-xslt-19991008) version of XSLT and is available at http://www.jclark.com/xml/xt.html. Like the other processors listed here, XT is free.


LotusXSL is a Java XSLT processor from IBM Alphaworks available at http://www.alphaworks.ibm.com. In November 1999 IBM donated LotusXSL to Apache, forming the basis for Xalan. LotusXSL continued to exist as a separate product. However, it is currently a thin wrapper around the Xalan processor. Future versions of LotusXSL may add features above and beyond those offered by Xalan, but there doesn’t seem to be a compelling reason to choose LotusXSL unless you are already using it.


The SAXON XSLT processor from Michael Kay is available at http://saxon.sourceforge.net. SAXON is open source software in accordance with the Mozilla Public License and is a very popular alternative to Xalan. SAXON provides full support for the current XSLT specification and is very well documented. It also provides several value-added features such as the ability to output multiple result trees from the same transformation and update the values of variables within stylesheets.

To transform a document using SAXON, first include saxon.jar in your CLASSPATH. Then type java com.icl.saxon.StyleSheet -? to list all available options. The basic syntax for transforming a stylesheet is as follows:

java com.icl.saxon.StyleSheet [options] source-doc style-doc [ params...]

To transform the presidents.xml file and send the results to standard output, type the following:

java com.icl.saxon.StyleSheet presidents.xml presidents.xslt


Version 1.1 of Sun’s Java API for XML Processing (JAXP) contains support for XSLT transformations, a notable omission from earlier versions of JAXP. It can be downloaded from http://java.sun.com/xml. Parsing XML and transforming XSLT are not the primary focus of JAXP. Instead, the key goal is to provide a standard Java interface to a wide variety of XML parsers and XSLT processors. Although JAXP does include reference implementations of XML parsers and an XSLT processor, its key benefit is the choice of tools afforded to Java developers. Vendor lock-in should be much less of an issue thanks to JAXP.

Since JAXP is primarily a Java-based API, we will cover its programmatic interfaces in depth as we talk about XSLT programming techniques in Chapter 5. JAXP currently includes Apache’s Xalan as its default XSLT processor, so the Xalan instructions presented in Chapter 2 will also apply to JAXP.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required