XSLT

Author : Doug Tidwell

473 pages

Publisher : O'Reilly & Associates

ISBN : 0-596-00053-7

Overview

One of the great promises of XML was that it would facilitate the exchange of

textual data between applications. Different applications tend to use different

data models, however, so when applications store or can produce their data in

XML format, these differences are mirrored in the structure of the XML that they

generate. XSLT (the eXtensible Stylesheet Language: Transformations) can be

used to convert data from one XML vocabulary to another (or to other text-based

formats), which means that it can help us bridge the gaps between applications

and get them talking to each other. In this new title from O'Reilly, Doug Tidwell,

a Senior Programmer / XML Evangelist at IBM, tackles XSLT in nine chapters and four

appendices.

The first chapter, "Getting Started", begins with a brief discussion of the design of

XSLT and, to a lesser extent, XPath and the problems that they were created to

address as well as giving readers a quick grounding in XML (well-formed vs. valid

documents, "tags" vs. elements, the difference between the DOM and SAX APIs

for parsing XML, etc.). Chapter Two is built around an elementary Hello World

example that Tidwell uses to introduce XSLT basics and includes a "Sample Gallery"

where the stylesheets used to transform the Hello World document into an SVG file, a

PDF (by way of XSL Formatting Objects), Java source code, and a simple VRML file

are shown along with the output. Next up is an entire chapter devoted to XPath, the

syntax used by both XSLT and Xpointer (another W3C initiative that just entered

candidate recommendation status last week: http://www.w3.org/TR/xptr/) to

address parts of XML documents.

The next four chapters cover specific aspects of XSLT. "Branching and Control Elements" deals with xsl:if, xsl:choose, and xsl:for-each and the ins and outs of invoking templates and defining and passing parameters to templates. Variables in XSLT (xsl:variable) are also explained and the chapter concludes with several examples that illustrate applications of XSLT templates. A section titled "Using Recursion to Do Almost Anything" walks readers through some string replace operations (the design problem being the transformation of a glob of XML into a set of SQL statements). The two other examples deal with the emulation of for loops in XSLT stylesheets. Chapter 5, "Creating Links and Cross-References", discusses the

comparative merits of using the XPath id() function in concert with element IDs in the input XML vs. XSLT's key() function to create links between different parts of an output HTML document (links to named anchors). "Sorting and Grouping Elements" deals with xsl:sort and strategies for grouping sorted elements. Chapter 7, "Combining XML Documents", covers the document() function.

The next to last chapter deals with extension elements and functions (a mechanism for adding new functions and elements to XSLT via stylesheet processors' APIs) and fallback processing (what's supposed to happen when and if the extensions called for aren't available). Chapter Nine is a case study, describing the development of a tutorial-publishing app used at IBM developerWorks that outputs linked HTML files (with bitmap images), PDF files, and a zip file. XSL-FO and extensions are used. Four appendices (an XSLT elements guide, a short but sweet XPath reference, a combined XSLT and XPath functions reference, and an "XSLT Guide" that functions as a FAQ) account for the remainder of the book.

What I liked

Although Tidwell emphasizes a real-world approach to tackling XSLT and includes plenty of practical examples in the book, he doesn't skimp when it comes to delivering the types of conceptual explanations (sections like "How a Stylesheet is Processed" in Chapter 2 and "The XPath View of an XML Document" in Chapter 3) that help readers understand what's going on "under the hood". Including a separate chapter covering the basics of XPath early in the book also makes for much easier reading, since we aren't left scrounging for scraps of information scattered throughout the text when XPath-related questions arise. If not for the fact that some authors have actually taken the opposite approach (introducing XPath concepts as they arise in the context of a discussion of XSLT), this would have seemed like a no-brainer.

XSLT is, for the most part, tool-agnostic, but there's more than a whiff of Java in some places and Xalan is the XSLT processor that's used when processor-specific topics are taken up (mostly in the final two chapters). Tidwell compensates for the slight Java and Xalan bias when, in addition to the Java code that implements the extension functions examined in Chapter 8, he emphasizes the fact that they can be written in other languages as well (extensions in Jython, Javascript, and Jacl are considered briefly). Tidwell also furnishes an example that illustrates how the system-property() function can be used to write portable XSLT - by defining different versions

of the extension for different processors and branching with xsl:when depending on the processor vendor (Saxon and XT are the other two processors accommodated).

Finally, no review of XSLT would be complete without mentioning the value added to the book by the appendices. Once you've digested all of the material in the body of the text, you'll likely continue to keep Tidwell's book close at hand because of Appendices A and C. Appendix A, the XSLT Reference, features a comprehensive dictionary-style reference for every element in XSLT 1.0 - including an XML source document, an example stylesheet that makes use of the element, and the result of the transformation for each. Appendix C, the XSLT and XPath Function Reference, follows a similar format.

The bottom line

This book is a must-have for developers working with XML and XSLT.