Xslt By Doug Tidwell
Posted on 22 Sep 2001
by brian donovan (briandonovan)
Rated 3.63 (Ratings: 10)
- More articles in Reviews & Links
Publisher : O'Reilly & Associates
ISBN : 0-596-00053-7
OverviewOne 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 ofXSLT 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 thecomparative 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.
The bottom line
This book is a must-have for developers working with XML and XSLT.