Java and XSLT

Overview

The power of XSLT is its ability to change the structure or format of any content that can be converted to XML. Java and XSLT shows you how to use XSL transformations in Java programs ranging from stand-alone applications to servlets. After an introduction to XSLT, the book focuses on applying transformations in some real-world scenarios, such as developing a discussion forum, transforming documents from one form to another, and generating content for wireless devices.

Java and ...

See more details below
Paperback (1ST)
$31.78
BN.com price
(Save 20%)$39.95 List Price
Other sellers (Paperback)
  • All (40) from $1.99   
  • New (9) from $21.95   
  • Used (31) from $1.99   
Sending request ...

Overview

The power of XSLT is its ability to change the structure or format of any content that can be converted to XML. Java and XSLT shows you how to use XSL transformations in Java programs ranging from stand-alone applications to servlets. After an introduction to XSLT, the book focuses on applying transformations in some real-world scenarios, such as developing a discussion forum, transforming documents from one form to another, and generating content for wireless devices.

Java and XSLT discusses several common XSLT processors and the TRAX API, paying special attention to performance issues. Although there's a brief tutorial introduction to the XSLT language, the primary focus of the book isn't on learning XSLT or developing stylesheets; it's on making practical use of transformations in Java code.

The book covers:

  • Introduction and Technology Review
  • XSLT—The Basics
  • XSLT—Beyond The Basics
  • Java Web Architecture
  • Programmatic Interfaces to XSLT Processors
  • Using XSLT with Servlets
  • Discussion Forum Implementation
  • Advanced XSLT Web Techniques
  • Testing, Tuning and Development Environments
  • WAP and WML
  • XSLT and Wireless Examples

Read More Show Less

Product Details

  • ISBN-13: 9780596001438
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 10/14/2001
  • Edition description: 1ST
  • Edition number: 1
  • Pages: 528
  • Sales rank: 1,098,494
  • Product dimensions: 6.70 (w) x 9.58 (h) x 1.22 (d)

Meet 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 More Show Less

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



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.

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...

Read More Show Less

Table of Contents

  • Preface
  • Chapter 1: Introduction
  • Chapter 2: XSLT Part 1 — The Basics
  • Chapter 3: XSLT Part 2 — Beyond the Basics
  • Chapter 4: Java-Based Web Technologies
  • Chapter 5: XSLT Processing with Java
  • Chapter 6: Servlet Basics and XSLT
  • Chapter 7: Discussion Forum
  • Chapter 8: Additional Techniques
  • Chapter 9: Development Environment, Testing, and Performance
  • Chapter 10: Wireless Applications
  • Discussion Forum Code
  • JAXP API Reference
  • XSLT Quick Reference
  • Colophon

Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star

(0)

4 Star

(0)

3 Star

(0)

2 Star

(0)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)