Accessibility
 
 
Exploring XML and XSL with JRun Custom Tags

Kevin Hoyt
Senior Sales Engineer
Macromedia, Inc.

Download the WAR file for this article.

Macromedia JRun Server 3.1 includes a powerful set of 25 JSP custom tags commonly referred to as the JRun Tag Library (JTL). While the idea of custom tags has been an asset to Macromedia ColdFusion for many years, the concept is new to J2EE in JSP 1.1. One of the benefits of JRun is that you can accomplish a wide variety of tasks with the JTL right out of the box.

Emerging standards outside of J2EE also lend powerful capabilities to developers in the form of tag-based description through Extensible Markup Language (XML). JRun, as well as other Java Application Servers, makes extensive use of XML to describe Web applications, environment variables, and deployment descriptors. As Java is to platform-neutral development, XML is to architecture-independent data exchange.

While developers can use XML to describe just about anything, it lacks any programmatic capabilities such as flow control or conditional logic. For XML, this is where the Extensible Stylesheet Language (XSL) offers a helping hand. Through the syntax of XSL, you can transform raw data in the form of XML into just about any other XML compliant form. JRun uses XSL to convert XML descriptors to information useful for its own operation.

Throughout this article, we will take a look at a few of the XML- and XSL-related custom tags found in the JTL. We will:

  1. Retrieve data through the use of the SQL tag
  2. Format that data into valid XML
  3. Convert that XML using XSL into an XML-compliant (XHTML) document

The best part about this is how easy JRun makes this otherwise complex series of tasks.

Getting Data With the SQL Tag
The first thing we need to do before we can start formatting and massaging data, is to retrieve data to work with. The JTL makes this a breeze through the SQL tag.

To use the SQL tag, or any custom tag in the JTL, a taglib directive needs to be declared toward the beginning of the JSP. To use JRun's Custom Tag Library, the taglib directive is as simple as:

<%@ taglib uri="jruntags" prefix="jrun" %>

The SQL tag can take three forms. Via parameter, we can pass the SQL tag an active Connection object, a JRun data source, or a driver and URL in the form of strings. Each has its own benefits. For this discussion, we will assume that a database containing some data exists, and that we are going to access that data through the SQL tag by passing it a driver name and a URL. Depending on your configuration, you may need to specify a username, password, or scope - all of which can be passed as parameters. In its most simple manifestation, a connection to a database might be as straightforward as:

<jrun:sql driver="sun.jdbc.odbc.JdbcOdbcDriver" 
		  url="jdbc:odbc:snippets" 
		  id="rs">
	SELECT *
	FROM Employees
</jrun:SQL>

Formatting Data With the Query2Xml Tag
It's that easy. A handful of keystrokes, and we've connected to a database and retrieved some data. But now that we have data, what do we do with it? Well, in our case, we want to create an XML document containing the data we have accessed. We only need to take a few more steps - the first of which is making our JSP look like a valid XML document.

By default, our JSP will set a content type of text/html. While this is fine for most purposes, an XML document requires a content type of text/xml. We can change this by adding a page directive, and setting the contentType attribute as follows:

 
<%@ page contentType="text/xml" %>
 

Now we just need to take our data and make XML out of it. To do this, we will reach into our JTL bag of tricks once more and pull out the Query2Xml tag. The only required attribute on the Query2Xml tag is the query attribute telling it which query to use. If you have been following my examples up to this point, you will just need to add the following line as the last entry in your JSP:

<jrun:query2xml query="RS"/>

Like the SQL tag, the Query2Xml tag can take several additional parameters for extra power. Most notably, the rootname and rowname attributes help to customize the resulting XML document a little further than the default.

As a review, let's look at the complete listing so far. You can cut and paste this into your own JSP, and with changes only to the URL and SQL statement, you'll be up and running with XML. Viewing the JSP in Internet Explorer will show you just how far you've come with XML in such a short amount of time thanks to JRun custom tags.

<%@ page contentType="text/xml" %>
<%@ taglib uri="jruntags" prefix="jrun" %>

<jrun:SQL driver="sun.jdbc.odbc.JdbcOdbcDriver" 
		  url="jdbc:odbc:snippets" 
		  id="RS">
	SELECT *
	FROM Employees
</jrun:SQL>

<jrun:query2xml query="RS"/>

Making the Most of XML With XSL
Now XML is great, but raw data and its description just does not help us much. For example, it is very likely that the data in our database - and the corresponding XML - will have a different format than what the requesting client will need. To change one format to another, we can leverage XSL and more specifically Extensible Stylesheet Language Transformations (XSLT).

No, additional software is not necessary. You don't need to run out and find a vendor for an XSL engine. All you have to do is reach back into the JTL bag-of-tricks and pull out the good old XSLT tag. And the good news is that despite all the intimidating acronyms and terminology we are tossing around, the JRun XSLT tag is the simplest one we have used yet.

The XSLT tag, like the SQL tag, is a Body Tag - that is, it has an open and close tag. All we need to do is wrap the Query2Xml tag from the previous example with the XSLT tag, and specify an XSL template to use against the raw XML. With that in mind, let's review a simple XSL template that will be applied to the generated XML to create a valid XHTML document.

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

<xsl:template match="/">

<html>
<head>
	<title>Employees</title>
</head>
<body>

Employee List (XSL)<br/>
<xsl:for-each select="table/row">
<xsl:value-of select="LastName"/>,
<xsl:value-of select="FirstName"/> -
<xsl:value-of select="EMail"/> -
<xsl:value-of select="Phone"/> -		
<xsl:value-of select="Department"/><Br/>
</xsl:for-each>

</body>
</html>

</xsl:template>
</xsl:stylesheet>

This is all pretty straightforward XSL. First and foremost, we are declaring this as a stylesheet, and then we set up the template. Beyond that, there's some XHTML setup, and then a little more XSL.

The format of the XML generated for us by default contains a series of row elements surrounded by a table element. We can tell the XSL for-each to use these elements for iteration control in the select attribute. Inside each row element in the XML are a series of elements that correspond to column names. Using the XSL value-of, we can instruct the transformation engine to display a value corresponding to a specific column in the current iteration. Close the respective tags, and our transformation template is complete.

There isn't much XHTML in this template, although we could have easily added tags for layout and CSS for formatting. Going a step further, with some slight modifications, we could add an XSL template to our repertoire for generating WML, or any other XML vocabulary.

Going back to our JSP, all that is needed is to wrap the XML that will be generated by the Query2Xml tag with the XSLT tag. The resulting JSP code then looks like the following:

<jrun:xslt xsl="html.xsl">
	<jrun:query2xml query="RS"/>
</jrun:xslt>

As a review, what is being accomplished with such a little amount of effort is amazing. This code queries a database and returns a result set. It transforms the result set into raw XML and processes the XML using XSL to create a valid XHTML document. That may seem like extra work to create an XHTML document, but think of the implications of abstracting the presentation layer and abstracting data in your applications - even worse, think of all the code you would have had to write to do it yourself.

Conclusion
JRun offers a wealth of productivity gains in many forms. One of the most powerful is the set of 25 JSP custom tags called the JRun Tag Library (JTL) that are included with JRun and function out-of-the-box. With only a little training, Web designers can be taught to use portions of the JTL to enhance their own abilities. But perhaps the greatest gain lies in the hand of the JSP developer.

In a few simple lines of code, the JSP developer can leverage the JTL to rapidly extend their own abilities and easily manipulate some of the most cutting edge and complex technologies available. The simple example presented in this article deals with database access, XML, and XSL as though it was child's play thanks to the JTL.

Throughout this article, I've assumed that the reader is familiar with most, or all, of the technologies discussed. If you need more information on XML or XSL, I highly recommend "XML Bible" by Elliotte Rusty Harold available from IDG Books, Inc. For JSP 1.1 custom tags, check out "Professional Java Server Programming: J2EE Edition" available from Wrox Press, Ltd., which also includes a plethora of additional knowledge about the platform on the server side.

Last but not least, check the JRun documentation. There are several hundreds of pages of PDF documentation on JRun, JSP, servlets, database access, custom tags, EJB, JMS, and much, much more right at your fingertips.