Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XSL gives your XML some style

Use XSL and servlets to style your XML data

  • Print
  • Feedback
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:

  • Print
  • Feedback

Resources