As you know, XML, by itself, merely provides the constructs necessary to describe data; it offers no information about how that data is to be presented to a user. Hence, a requirement arises for a separate stylesheet language to handle the presentation aspects of an XML application. This stylesheet language is the Extensible Stylesheet Language (XSL), and it consists of the following three components:
XML Path Language (XPath), which provides constructs to locate specific nodes or node collections in an XML document tree
XSL Transformations (XSLT), which is responsible for restructuring, or transforming, XML data into a different format
XSL Formatting Objects (XSL-FO), which is responsible for the formatting and presentation of the result.
The very first Working Draft of XSL appeared on the W3C's web site in August 1998. At that time, the specification combined XSLT, XPath, and XSL-FO into a single document; the three were later separated into independent entities. The XSLT and XPath specifications were approved by the W3C as W3C Recommendations in November 1999, with the XSL 1.0 specification achieving Recommendation status in October 2001. The W3C's XSL Working Group is currently working to develop the next versions of these specifications in order to address new XML technologies and develop new extensions to the language.
By separating the layout and presentation of data from the data itself, the XSL family of technologies makes it possible to create different representations and views of the same data, simply by applying a different stylesheet to the same XML source. For example, a single inventory statement that is marked up in XML could be combined with different stylesheets to create views customized to the requirements of a purchase manager (who needs to view stock quantities), an accountant (who needs to view costs), and a customer (who needs to view unit availability). In a similar vein, a separate presentation layer also makes it easier to export data into a variety of different formatsthe same XML source could therefore be used to generate ASCII documents, HTML web pages, and WML content.
This chapter focuses primarily on XSLT, which provides high-level constructs to transform a source XML document (referred to in the official specification as a source tree) into a new document (the result tree). It does this by using a stylesheet containing template rules to locate and match structures in the source tree, and transform them into new structures in the corresponding result tree.
Perhaps the best way to understand this is with a simple example. Consider the XML document in Listing 4.1, which contains a (fictional) list of best-selling books.
Listing 4.1 Simple XML Book List (list.xml)
<title>Waking Up With The Red-Eyed Bed Monster</title>
<author>J. I. M. Somniac</author>
<blurb>The blood-chillingly true story of one man's fight against the
monsters under his bed.</blurb>
<title>The Case Of The Hungry Hippopotamus</title>
<author>P. I. Hardhat</author>
<blurb>A tough private eye is hired to
solve the most challenging case of
<title>Making Money, Losing Friends</title>
<blurb>The bestselling self-help book
for corporate executives on the fast
<title>The Idiot's Guide to Sandwiches</title>
<author>B. L. Tuhmatto</author>
<blurb>Making tasty sandwiches has never been so easy!</blurb>
Listing 4.2 is its associated XSLT stylesheet.
Listing 4.2 XSLT Stylesheet (list.xsl)
<xsl:stylesheet version="1.0" xmlns:
<!-- set up the main page container -->
<!-- look for the list node -->
<xsl:template match="/list" xml:space="preserve">
<!-- iterate through the item nodes under the list node -->
<font face="Arial"><b><xsl:value-of select="title" /></b> - <xsl:value-of
<font face="Arial"><i><xsl:value-of select="blurb" /></i></font>
When I put the two together and run them through an XSLT processor, Figure 4.1 is what I get.
Figure 4.1. An HTML page created via an XSL Transformation.
The rules in the stylesheet are matched against the XML markup to create new HTML structures; the resulting HTML output is viewable in any web browser. If, for example, I decided to output WML rather than HTML, I would need merely to modify the presentation layer (the stylesheet) to reflect the new formatting required in the result tree with no modifications required to the original XML data source. (Take a look at Listing 4.20 for an example of how this works.)
This is a very simple exampleXSLT allows for a much greater level of complexity, both in creating template rules and in manipulating the result tree. I won't get into the details herethis chapter assumes a familiarity with the basics of XSLTalthough you can find links to a number of interesting articles and tutorials (for both novices and experts) on this book's companion web site.