Java and XSLT

Java and XSLT


$35.96 $39.95 Save 10% Current price is $35.96, Original price is $39.95. You Save 10%.
Choose Expedited Shipping at checkout for guaranteed delivery by Monday, January 21

Product Details

ISBN-13: 9780596001438
Publisher: O'Reilly Media, Incorporated
Publication date: 10/14/2001
Edition description: 1ST
Pages: 528
Product dimensions: 7.00(w) x 9.19(h) x 1.22(d)

About the Author

Burke is a Java consultant. Eric has a Bachelor of Science degree in Computer Science from Southern Illinois University at Carbondale and is currently Principal Software Engineer with Object Coomputing, Inc. in St. Louis, MO.

Read an Excerpt

Chapter 5: XSLT Processing with Java

Since many of the XSLT processors are written in Java, they can be directly invoked from a Java application or servlet. Embedding the processor into a Java application is generally a matter of including one or two JAR files on the CLASSPATH and then invoking the appropriate methods. This chapter shows how to do this, along with a whole host of other programming techniques.

When invoked from the command line, an XSLT processor such as Xalan expects the location of an XML file and an XSLT stylesheet to be passed as parameters. The two files are then parsed into memory using an XML parser such as Xerces or Crimson, and the transformation is performed. But when the XSLT processor is invoked programmatically, you are not limited to using static files. Instead, you can send a precompiled stylesheet and a dynamically generated DOM tree directly to the processor, or even fire SAX events as processor input. A major goal is to eliminate the overhead of parsing, which can dramatically improve performance.

This chapter is devoted to Java and XSLT programming techniques that work for both standalone applications as well as servlets, with a particular emphasis on Sun's Java API for XML Processing (JAXP) API. In Chapter 6, we will apply these techniques to servlets, taking into account issues such as concurrency, deployment, and performance.

A Simple Example

Let's start with perhaps the simplest program that can be written. For this task, we will write a simple Java program that transforms a static XML data file into HTML using an XSLT stylesheet. The key benefit of beginning with a simple program is that it isolates problems with your development environment, particularly CLASSPATH issues, before you move on to more complex tasks.

Two versions of our Java program will be written, one for Xalan and another for SAXON. A JAXP implementation will follow in the next section, showing how the same code can be utilized for many different processors.


CLASSPATH problems are a common culprit when your code is not working, particularly with XML-related APIs. Since so many tools now use XML, it is very likely that a few different DOM and SAX implementations reside on your system. Before trying any of the examples in this chapter, you may want to verify that older parsers are not listed on your CLASSPATH.

More subtle problems can occur if an older library resides in the Java 2 optional packages directory. Any JAR file found in the jre/lib/ext directory is automatically available to the JVM without being added to the CLASSPATH. You should look for files such as jaxp.jar and parser.jar, which could contain older, incompatible XML APIs. If you experience problems, remove all JAR files from the optional packages directory.

Unfortunately, you will have to do some detective work to figure out where the JAR files came from. Although Java 2 Version 1.3 introduced enhanced JAR features that included versioning information, most of the JAR files you encounter probably will not utilize this capability.

The Design

The design of this application is pretty simple. A single class contains a main( ) method that performs the transformation. The application requires two arguments: the XML file name followed by the XSLT file name. The results of the transformation are simply written to System.out. We will use the following XML data for our example:

<?xml version="1.0" encoding="UTF-8"?>
<message>Yep, it worked!</message>

The following XSLT stylesheet will be used. It's output method is text, and it simply prints out the contents of the <message> element. In this case, the text will be Yep, it worked!.

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:output method="text" encoding="UTF-8"/>
  <!-- simply copy the message to the result tree -->
  <xsl:template match="/">
    <xsl:value-of select="message"/>

Since the filenames are passed as command-line parameters, the application can be used with other XML and XSLT files. You might want to try this out with one of the president examples from Chapters and 3.

Xalan 1 Implementation

The complete code for the Xalan implementation is listed in Example 5-1. As comments in the code indicate, this code was developed and tested using Xalan 1.2.2, which is not the most recent XSLT processor from Apache. Fully qualified Java class names, such as org.apache.xalan.xslt.XSLTProcessor, are used for all Xalan-specific code.

TIP: A Xalan 2 example is not shown here because Xalan 2 is compatible with Sun's JAXP. The JAXP version of this program works with Xalan 2, as well as any other JAXP compatible processor....

...The code begins with the usual list of imports and the class declaration, followed by a simple check to ensure that two command line arguments are provided. If all is OK, then the XML file name and XSLT file name are converted into system identifier values...

Table of Contents

Software and Versions;
Conventions Used in This Book;
How to Contact Us;
Chapter 1: Introduction;
1.1 Java, XSLT, and the Web;
1.2 XML Review;
1.3 Beyond Dynamic Web Pages;
1.4 Getting Started;
1.5 Web Browser Support for XSLT;
Chapter 2: XSLT Part 1 -- The Basics;
2.1 XSLT Introduction;
2.2 Transformation Process;
2.3 Another XSLT Example, Using XHTML;
2.4 XPath Basics;
2.5 Looping and Sorting;
2.6 Outputting Dynamic Attributes;
Chapter 3: XSLT Part 2 -- Beyond the Basics;
3.1 Conditional Processing;
3.2 Parameters and Variables;
3.3 Combining Multiple Stylesheets;
3.4 Formatting Text and Numbers;
3.5 Schema Evolution;
3.6 Ant Documentation Stylesheet;
Chapter 4: Java-Based Web Technologies;
4.1 Traditional Approaches;
4.2 The Universal Design;
4.3 XSLT and EJB;
4.4 Summary of Key Approaches;
Chapter 5: XSLT Processing with Java;
5.1 A Simple Example;
5.2 Introduction to JAXP 1.1;
5.3 Input and Output;
5.4 Stylesheet Compilation;
Chapter 6: Servlet Basics and XSLT;
6.1 Servlet Syntax;
6.2 WAR Files and Deployment;
6.3 Another Servlet Example;
6.4 Stylesheet Caching Revisited;
6.5 Servlet Threading Issues;
Chapter 7: Discussion Forum;
7.1 Overall Process;
7.2 Prototyping the XML;
7.3 Making the XML Dynamic;
7.4 Servlet Implementation;
7.5 Finishing Touches;
Chapter 8: Additional Techniques;
8.1 XSLT Page Layout Templates;
8.2 Session Tracking Without Cookies;
8.3 Identifying the Browser;
8.4 Servlet Filters;
8.5 XSLT as a Code Generator;
8.6 Internationalization with XSLT;
Chapter 9: Development Environment, Testing, and Performance;
9.1 Development Environment;
9.2 Testing and Debugging;
9.3 Performance Techniques;
Chapter 10: Wireless Applications;
10.1 Wireless Technologies;
10.2 The Wireless Architecture;
10.3 Java, XSLT, and WML;
10.4 The Future of Wireless;
Discussion Forum Code;
JAXP API Reference;
XSLT Quick Reference;

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews