XML technologies / XSLT / XSLT Cookbook / Selecting and traversing
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
“I take a block of marble and remove everything unnecessary.”
François-Auguste Rodin
“I have traveled across this country and spoken to the best people,
and I assure you that computing is a fad
that will not last the year.”
Business-book editor at a major publisher, 1957
Introduction
Almost anything interesting in XML involves two related operations: deciding which nodes to visit (selection) and in what order (traversal). Selection is largely the task of XPath, a separate specification closely tied to XSLT. Traversal depends on XSLT control structures and how templates are organized to use them.
Veterans may find no exciting discoveries here, but the chapter matters for two reasons. First, these ideas distinguish XSLT from other languages and often confuse beginners. Second, these examples build the more complex recipes that follow: almost everything in XSLT involves selection and traversal. As in cooking, making brown stock is a prerequisite for espagnole sauce. This concentrated brown tomato sauce is thickened with roux and reduced; its stock comes from veal and beef shanks. Espagnole is needed for demi-glace, the mother of many brown sauces. Cooks surpass software developers at reuse! Master these stock-making examples before more ambitious XSLT applications.
Despite simple examples, this is not an XPath or XSLT tutorial. Know the XPath basics covered in XPath and the default XSLT rules for choosing subsequent templates. For help, consult Michael Kay’s XSLT Programmer's Reference (Wrox Press, 2004) or Evan Lenz’s XSLT 1.0 – kurz & gut (O’Reilly, 2006). Differences between 1.0 and 2.0 are smaller here because 1.0 already expressed most operations well. An exception is selection based on groups related by criteria other than XML hierarchy; the new xsl:for-each-group instruction is especially welcome for this.
Some examples use computer-science terminology for tree algorithms. XML defines trees for XSLT to process; indeed, an XSLT processor can process non-XML if a SAX driver makes it look like a tree. Michael Kay demonstrates this in XSLT Programmer's Reference, as does Eric M. Burke in Java und XSLT (O’Reilly, 2001). Thinking in trees reveals useful XML-processing techniques, motivating the chapter’s examples.
Many examples use these two test documents, shown here rather than repeated in every recipe: SalesBySalesPerson.xml and orgchart.xml.
Example: SalesBySalesPerson.xml
<?xml version="1.0" encoding="UTF-8"?>
<salesBySalesperson>
<salesperson name="John Adams" seniority="1">
<product sku="10000" totalSales="10000.00"/>
<product sku="20000" totalSales="50000.00"/>
<product sku="25000" totalSales="920000.00"/>
</salesperson>
<salesperson name="Wendy Long" seniority="5">
<product sku="10000" totalSales="990000.00"/>
<product sku="20000" totalSales="150000.00"/>
<product sku="30000" totalSales="5500.00"/>
</salesperson>
<salesperson name="Willie B. Aggressive" seniority="10">
<product sku="10000" totalSales="1110000.00"/>
<product sku="20000" totalSales="150000.00"/>
<product sku="25000" totalSales="2920000.00"/>
<product sku="30000" totalSales="115500.00"/>
<product sku="70000" totalSales="10000.00"/>
</salesperson>
<salesperson name="Arty Outtolunch" seniority="10"/>
</salesBySalesperson>Example: orgchart.xml
<?xml version="1.0" encoding="UTF-8"?>
<employee name="Jil Michel" sex="female">
<employee name="Nancy Pratt" sex="female">
<employee name="Phill McKraken" sex="male"/>
<employee name="Ima Little" sex="female">
<employee name="Betsy Ross" sex="female"/>
</employee>
</employee>
<employee name="Jane Doe" sex="female">
<employee name="Walter H. Potter" sex="male"/>
<employee name="Wendy B.K. McDonald" sex="female">
<employee name="Craig F. Frye" sex="male"/>
<employee name="Hardy Hamburg" sex="male"/>
<employee name="Rich Shaker" sex="male"/>
</employee>
</employee>
<employee name="Mike Rosenbaum" sex="male">
<employee name="Cindy Post-Kellog" sex="female">
<employee name="Allen Bran" sex="male"/>
<employee name="Frank N. Berry" sex="male"/>
<employee name="Jack Apple" sex="male"/>
</employee>
<employee name="Oscar A. Winner" sex="male">
<employee name="Jack Nicklaus" sex="male">
<employee name="R.P. McMurphy" sex="male"/>
</employee>
<employee name="Tom Hanks" sex="male">
<employee name="Forrest Gump" sex="male"/>
<employee name="Andrew Beckett" sex="male"/>
</employee>
<employee name="Susan Sarandon" sex="female">
<employee name="Helen Prejean" sex="female"/>
</employee>
</employee>
</employee>
</employee>
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de