Rendering a connected tree using XSLT: the ideas behind XSLT and XPath make perfect sense.(How-To)
Publication Date: 01-FEB-04
Publication Title: XML Journal
Format: Online
Author: Berry, Craig

Read this article now
Try Goliath Business News - FREE!

You can view this article PLUS...

  • Over 5 million business articles
  • Hundreds of the most trusted magazines, newswires, and journals (see list)
  • Premium business information that is timely and relevant
  • Unlimited Access

Now for a Limited Time, try Goliath Business News
Free for 7 Days!

Tell Me More   Terms and Conditions

Purchase this article for $4.95

Description

One of the things I enjoy most about working with XML and its related technologies is that there is always more to learn. I have been using XML for three years, but not a week goes by that I'm not pleasantly surprised to find something new I can do with it--or, even better, to find an easier way to do something I thought I had already figured out.

One such discovery occurred recently in connection with the rendering of "connected tree"--style hierarchies. A connected tree is one in which lines link nodes to their parents and children, as in many types of file system and outline displays. (See Figure 1 for an example.) The application we are currently building at PortBlue uses this type of display in many places for different purposes, making it difficult to unify handling of the rendering process across the varying types of data in the hierarchies. However, all the hierarchy types are available as DOM trees. It struck me that if I could find a way to render connected trees using only XSLT, I could remove all the application code associated with tree rendering and use a single stylesheet to handle all the tree rendering, which would vastly simplify our code.

[FIGURE 1 OMITTED]

Implementing this idea has resulted in several hundred lines of bug-prime Java rendering code (spread across many classes) being replaced with a few dozen lines of XSL in a single stylesheet, improving our system's reliability, maintainability, and ease of customization. In this article, I'll explain how we did it.

As I'm sure most readers are already aware, XSL and XSLT are paired standards that specify how to describe a transformation between XML and some other desired format for the same data. An XSL stylesheet provides a recipe for finding data in the source XML data, transforming it, and outputting the transformed version. The technology has a multitude of uses, but probably the most commonly encountered application is the transformation of XML data into appropriate HTML for presentation to a human user.

Yet another specification, called XPath, is used extensively in XSLT. XPath is a functional language for locating information in an XML tree structure. Its role in XSLT is as a structural pattern-matching tool for finding the data that is to be transformed into particular parts of the generated document.

For me, at least, XPath has been tire hardest part of the XSLT toolkit...



Looking for additional articles?
Click here to search our database of over 3 million articles.