ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Developing, Applying and Optimizing XSLT with Java Servlets

by Eric M. Burke

This article explains the basic patterns and programming techniques commonly used when XSL Transformations (XSLT), Java Servlets, and XML are combined to create sophisticated web applications. The types of applications that benefit from this approach include

  • Web sites that need to target multiple incompatible browsers or perhaps wireless devices;
  • Web applications that need to provide XML data feeds in addition to HTML interfaces;
  • Complex web applications where it makes sense to design in a very modular way, enforcing a clean separation between data, presentation, and programming logic; and
  • Web applications that need to target different languages and character sets from the same data model.

It is hard to imagine a situation where XML and XSLT would not work, often more elegantly than JSP or pure servlet approaches.

XSLT basics

XSL stands for Extensible Stylesheet Language which is a two-part specification from the Worldwide Web Consortium. XSL Formatting Objects, which is not covered by this article, is an XML language for specifying the formatting of documents, such as fonts, colors, and alignments. At the time of this writing, XSL Formatting Objects is a Working Draft and is subject to change. Current web browsers do not support XSL Formatting Objects, so this is not a viable technology for the foreseeable future.

The second part of XSL is XSLT, which was designed to transform well-formed XML documents into XSL Formatting Objects documents. XSLT is a W3C Recommendation, which is equivalent to saying that it is a standard. Although XSLT was designed to support XSL Formatting Objects, it works well as a general purpose XML transformation language. When people talk about XSL, they are usually referring to XSLT, which is much more widely used today.

The following are the basic elements of XSLT.

  • An XSLT stylesheet, conforming to the XSLT 1.0 Recommendation.
  • An XML input source, which also must be well-formed.
  • An XSLT processor, which is an application that knows how to parse XSLT stylesheets and apply transformations. The servlet example in this article will use Apache's Xalan, an open source processor from the Apache Software Foundation.
  • A result tree, which is the output from the XSLT processor.
XSLT Transformation Process.

Figure 1: XSLT Transformation Process.

As Figure 1 illustrates, the XML input is transformed into something called a result tree. The result tree could be another XML file, an HTML web page, or even a plain text file. Nothing binds the XML input to the XSLT stylesheet, so there is a very clear separation between data and formatting. This is a boon for servlet programmers, because you can easily target multiple incompatible browsers by simply supplying two different stylesheets. You could also target Wireless Markup Language (WML), again via another stylesheet. This is much more attractive than traditional approaches which require changes to programming logic to support multiple targets.

Example 1: Basic XSLT Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  <xsl:output method="html"/>

  <xsl:template match="/">
        <xsl:apply-templates select="creditInfo"/>

  <xsl:template match="creditInfo">
    Name: <xsl:value-of select="name"/>
    Type: <xsl:value-of select="type"/>
    Number: <xsl:value-of select="number"/>
    Expires: <xsl:value-of select="expires"/>

The stylesheet shown in Example 1 will be used later in this article to transform an XML document using a servlet. The root element of an XSLT stylesheet specifies the version and namespace. The only version available at this time is 1.0, and the namespace sets up the xsl: prefix for all XSLT elements. The next element specifies that the output document is HTML, although this line is optional in this case.

Comment on this articleShare your experiences with XSLT.
Post your comments

The remainder of the stylesheet consists of two <xsl:template ...> elements. The job of a template is to match patterns in the XML input. The first template typically matches /, which is the XML document itself. The content inside of the template is then copied to the result tree except for <xsl:apply-templates ...>.

The apply-templates element causes additional pattern matching to occur in the source document, this time within the context of the current template. Since XML documents form a tree data structure, XSLT processing follows a recursive process of matching patterns with <xsl:template>, and then recursively searching down the tree data structure using <xsl:apply-templates>. In our example above, the root element of the XML is <creditInfo>, which contains <name>, <type>, <number>, and <expires> elements.

Of course, a complete explanation of XSLT is well beyond the scope of this article, but this should give you a flavor of what stylesheets generally look like (see the side bar for links to more detailed information). You may be wondering why the HTML <br/> tag has the / character in it. This is required because the XSLT stylesheet must be well-formed XML. Omitting the / character would cause a parsing error. Since the output method is set to HTML, Xalan removes the extra character, formatting <br> as normal HTML.

Pages: 1, 2, 3

Next Pagearrow