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
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:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<!-- simply copy the message to the result tree -->
<xsl:template match="/">
<xsl:value-of select="message"/>
</xsl:template>
</xsl:stylesheet>
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.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|