JIM HUNTER Computer Software & Web Application Development

A couple of cool ideas for XSLT:

A number of years ago I came across an article, "XSLT Querying & XML Documents," by Giuseppe Naccarato, in Dr.Dobb's Journal, December 2002. In this article Mr. Naccarato described a relatively new use of XSLT (i.e., new at that time, at least!), as a mechanism for generating dynamic content.

I realized that I could use the same approach in developing web applications. One application that I had previously created - the Message Editor application - accomplished a similar task by parsing an XML document and pulling out a specific set of nodes for display in a web browser. The task was accomplished with the aid of the Java SAX API and a custom data structure to store the data from the XML file. The data structure I needed to create required much additional coding on my part, as well as the org.xml.sax.ContentHandler implementation.

Naccarato's article pointed to a simpler approach that had advantages over SAX or DOM centric strategies. This idea inspired me to jump into the world of XSLT. But first I had to learn basic XSLT. My first task was to create a static page, so I re-composed my online resume. Since a resume breaks down into relatively uniform blocks of text, this seemed like a suitable application for XSL styling. I decided to encapsulate just those uniform blocks of text into XML data. Then I could pull out that information and insert it into a JSP page. So I created resume_data.xml to store my data and resume_template.xsl to style that data. These two files are passed to the XSLT processor which then produces my online resume. The Java code to invoke the XSLT processor is not too bad:

StreamSource xslSource = 
    new StreamSource(new File("<path_to_file>/resume_template.xsl"));
StreamSource xmlSource = 
    new StreamSource(new File("<path_to_file>/resume_data.xml"));

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xslSource);
transformer.transform(xmlSource, new StreamResult(out));

Basically you create a Transformer object, passing to the constructor your XSL file. Then you give the Transformer object an XML file to transform and tell it where to send the output, which in this case is a java.io.OutputStream.

After getting my feet wet, creating a static page - my online resume - I applied the XSLT approach to my Message Editor application to generate user selected content dynamically. In the process I discovered that the XSLT transformer can accept Java objects as parameters, to be used during the transformation process. Java objects can also be declared in the XSL file and used as customized extensions to the built-in functionality. Here's a diagram of my application:

Message Editor Diagram

The Web client request is handled by MessageEditorServlet, which creates the MessageManager, stores it in the session, and hands it off to the XSLT transformer. MessageManager is responsible for reading the messages from a properties file and providing operations to retrieve and modify those messages. The transformer uses MessageManager as well as MessageEditorXsltUtil, which is an extension class. MessageEditorXsltUtil provides utility operations for text formatting, abbreviation, etc, which are more easily implemented in a programming language like Java. Using this approach the transformation process can be augmented to include operations that any Java object can execute.

If interested in the original article by Giuseppe Naccarato you can check it out [here]. My letter to DDJ in response to the article should be found [here]. You may need to scroll down to "Happy with XSLT."

XSLT has a variety of advantages and is suitable for a number of tasks that would be more tedious otherwise. XSLT can also be used in conjunction with JSTL tags, as well, to produce consistent and reusable HTML elements. I found it useful in generating the HTML for hyperlinks throughout this Web site. It's superior to the cut-and-paste method, which can be tedious and error-prone. The resulting HTML code is much less cluttered, and you only need to edit the links in one place, an XSL stylesheet, to modify every occurence of the affected HTML element. So, for instance, consider this XSL file fragment:

<xsl:variable name="popupMsg">You may need to disable your Pop-up Blocker to view</xsl:variable>
<xsl:template match="a[@id='messageEditor']">
    <a title="{$popupMsg}" href="javascript:popUp('/ajax/msgEditorAjax.jsp','',defaultSpecs)">Message Editor</a>
The template defined here can be applied to any tag matching <a id="messageEditor"/>. So then the JSTL XSLT tags can be used in a JSP file to access the XSL stylesheet and inject HTML into the page. First, the XSL stylesheet is imported into the JSP.
<c:import url="/xsl/jstl-transform.xsl" var="xslFile"/>
Then that stylesheet can be applied via the JSTL XSLT tag.
<x:transform xslt="${xslFile}"><a id="messageEditor"/></x:transform>
The <a id="messageEditor"/> tag is treated as XML by the XSLT transformer and the entire tag is substituted for the HTML code defined in the XSL stylesheet. The resulting HTML looks like this:
<a title="You may need to disable your Pop-up Blocker to view" href="javascript:popUp('/ajax/msgEditorAjax.jsp','',defaultSpecs)">Message Editor</a>
The JSP code is much cleaner as a result, and much easier to maintain. An additional advantage is that all the Web site links can be stored and maintained in one file, which is extremely useful if many of the links appear in multiple locations throuhout the Web site.
copyright © 2003 - 2017 James P Hunter