Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

XSLT blooms with Java

Use Java in your stylesheets when XSLT won't do the trick

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

XSLT's main weakness is text processing, which seems reasonable since its purpose is to render XML. However, because XML content is entirely text, XSLT needs stronger text handling. Needless to say, stylesheet designers require some extensibility from time to time. With Xalan, Java provides this extensibility.

Use JDK classes within XSLT

You might be pleased to know that you don't have to write any Java code to take advantage of Xalan's extensibility. When you use Xalan, you can create and invoke methods on almost any Java object. Before using a Java class, you must provide an XSLT namespace for it. This example declares "java" as a namespace for everything in or under the Java package (i.e., the entire JDK):

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:java="java" >


Now we need something to do. Let's start with a small XML document:

<article>
      <title>Java May Be a Fad</title>
      <author>J. Burke</author>
      <date>11/30/97</date>
</article>


You've been asked to style this XML so the title appears in uppercase. A developer new to XSLT would simply pop open an XSLT reference to look for the toUpper() function; however, she'd be disappointed to find that the reference lacks one. The translate() method is your best bet, but I have an even better method: java.lang.String.toUpperCase(). To use this method, you need to instantiate a String object with the title contents. Here is how you can create a new String instance with the title element's contents:

<xsl:template match="title">
      <xsl:variable name="titleStr" select="java:lang.String.new(.)"/>


The name attribute specifies the handle to your new String instance. You invoke the constructor by first specifying the namespace along with the remaining path to the String class. As you might have noticed, String lacks a new() method. You use new() to construct a Java object in Xalan; it corresponds to Java's new keyword. The arguments given to new() determine the constructor version that will be called. Now that you have the title contents within a Java String object, you can use the toUpperCase() method, like so:

  <xsl:value-of select="java:toUpperCase($titleStr)"/>


This might look strange to you at first. When using Java methods on a particular instance, the first argument is the instance you want the method invoked on. Obviously Xalan uses introspection to provide this capability.

Below you'll find another trick. Here is how you might emit the date and time anywhere within your stylesheet using java.lang.Date:

  <xsl:value-of select="java:util.Date.new()"/>


Here's something that will make the day of anyone required to localize a generic stylesheet between two or more languages. You can use java.util.ResourceBundle to localize literal text within a stylesheet. Since your XML has an author tag, you might want to print "Author:" next to the person's name.

One option is to create a separate stylesheet for each locale, i.e., one for English, another for Chinese, and so on. The problems inherent in this approach should be evident. Keeping multiple stylesheet versions consistent is time consuming. You also need to modify your application so that it chooses the correct stylesheet based on the user's locale.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources