Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

XSL gives your XML some style

Use XSL and servlets to style your XML data

Wow, you've come so far. A year ago you didn't know what XML was, but now it's everywhere. You're storing XML in databases and using it in middleware, and now you want to present that data to a browser. That's where XSL can help. XSL can help you turn your XML into HTML. Moreover, servlets provide a powerful medium to perform those translations as they reside on the server and have access to all the features of server-side Java.

In this article, we'll cover the basics of XSL and XSL processors. If you don't know much about XML, you may want to first read Mark Johnson's excellent XML article, "Programming XML in Java, Part 1."

Our example will use a servlet to turn well-formed XML into HTML. If you need to learn more about servlets, please refer to Sun's servlets tutorial (see Resources).

XML and XSL

The process of transforming and formatting information into a rendered result is called styling. Two recommendations from the W3C come together to make styling possible: XSL Transformations (XSLT), which allows for a reorganization of information, and XSL, which specifies the formatting of the information for rendering.

With those two technologies, when you put your XML and XSL stylesheet into an XSL processor, you don't just get a prettied up version of your XML. You get a result tree that can be expanded, modified, and rearranged.

An XSL processor takes a stylesheet consisting of a set of XSL commands and transforms it, using an input XML document. Let's take a look at a simple example.

Below we see a small piece of XML, describing an employee. It includes his name and title. Let's assume that we would like to present that in HTML.

<employee id="03432">
  <name>Joe Shmo</name>
  <title>Manager</title>
</employee>


If we wanted our HTML to look like this:

<html>
  <body>
    <p><b>Joe Shmo</b>: Manager</p>
  </body>
</html>


Then we could use a stylesheet, such as the one below, to generate the HTML above. The stylesheet could reside in a file or database entry:

<xsl:stylesheet xmlns:xsl="">
  <xsl:template match="/">
    <html>
      <body>
        <p>
          <b>
            <xsl:value-of select="employee/name"/>
          </b>
          <xsl:text>: </xsl:text>
          <xsl:value-of select="employee/title"/>
        </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


Common XSL stylesheet commands

Stylesheets are defined by a set of XSL commands. They make up valid XML documents. Stylesheets use pattern matching to locate elements and attributes. There are also expressions that can be used to call extensions -- either Java objects or JavaScript. Let's look at some XSL commands.

Stylesheet declaration

The stylesheet declaration consists of a version and namespace. The namespace declares the prefix for the tags that will be used in the stylesheet and where the definition of those tags are located:

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


If there are any extensions referenced, the namespace must be specified. For example, if you were going to use Java, you would specify this namespace:

xmlns:java="http://xml.apache.org/xslt/java"


Pattern matching

When selecting in a stylesheet, a pattern is used to denote which element or attribute we want to access. The syntax is simple: specify the node you want, using / to separate the elements.

Notice that in the sample XML code above we matched our template on /, which is the root node. We could have, however, matched on the employee node. Then a select statement could just refer to the name node instead of employee/name.

For example, if we had the following XML:

<employee id="03432">
  <name>Joe Shmo</name>
  <title>Manager</title>
</employee>


Attributes can also be selected. The employee id could be accessed by saying employee/@id. Groups of nodes can be accessed by using employee/*. A specific employee could be located using employee/@id='03432'.

Pattern matching allows us to select specific values out of our XML document. The <xsl:value-of select... command gives us the ability to select a value for our resulting XML document, as seen in the table below.


Accessing elements versus attributes
Command Result
<xsl:value-of select="employee/name"/> Joe Shmo
<xsl:value-of select="employee/@id"/> 03432

Templates



Templates provide a way to match nodes in an XML document and perform operations on them. The syntax for a template is:

<xsl:template match="nodename">
.
.
.
</xsl:template>


The template is matched on a node name, then all the stylesheet commands in that template are applied. We can call templates in our stylesheet by using the apply-templates command:

<xsl:apply-templates select="nodename"/>


An example using our employee XML above would be:

<xsl:template match="name"
  <xsl:value-of select="."/>
</xsl:template>


We can call this template anywhere there is a name node to be referenced, using this:

<xsl:apply-templates select="name"/>


Logical commands

There are a few structures available for doing ifs and loops. Let's take a look at the syntax.

Choose command

The choose command provides a structure to test different situations.

<xsl:choose>
  <xsl:when test="test situation">
    stylesheet commands
  </xsl:when>
  <xsl:otherwise>
    stylesheet commands
  </xsl:otherwise>
</xsl:choose>


The first successful test will result in that block's stylesheet commands executing. If all the tests fail, the otherwise block is executed. You may have as many when blocks as you want. The otherwise block must always be present; if you don't want to do anything in your otherwise block, just put:

<xsl:otherwise/>


If command

The if command provides only a single test and doesn't have any kind of else structure available. If you need to have an else, use the choose command.

<xls:if test="test situation">
...
</xsl:if>


Loops (for-each command)

Unlike most languages with for and while structures, XSL offers only the for-each command. As such, you can loop on a set of nodes or you can select the nodes you want to loop on, using a pattern match:

<xsl:for-each select="select statement">
...
</xsl:for-each>


For example, if you had more than one employee in your XML document, and you wanted to loop through all the managers, you could use a statement such as this:

1 | 2 | 3 |  Next >
Resources