Introduction


Doing anything remotely interesting in XSLT involves two related operations: determining which XML nodes to visit (selecting) and determining in what order you want to visit them (traversing). Selecting is largely in the domain of XPath, a separate specification but one intimately related to XSLT. Traversing is a function of built-in control structures of XSLT and how you organize your templates to harness them.

XSLT veterans are unlikely to find much revelation in this particular chapter. Nevertheless, it is important for two reasons. First, the ideas presented in these recipes distinguish XSLT from other programming languages and therefore tend to be things that trip up novices on their first attempts to master XSLT. Second, the examples covered in this section are the primitive building blocks of many more complex recipes covered in later chapters. Virtually everything one does in XSLT involves application of selection and traversal. By analogy to cooking, knowing how to make a good brown stock is a prerequisite to making a sauce espagnole![1] This chapter is comprised of examples of the "brown stock" variety and should be mastered before proceeding to more advanced applications of XSLT.

[1] Sauce espagnole is highly concentrated brown stock with tomatoes bound with roux and reduced. The brown stock is made from veal and beef shanks. Sauce espagnole is necessary for making demi-glace, the "mother sauce" for many other brown sauces. When it comes to "reuse," the chefs have the software developers beat!

Although this chapter presents some primitive examples, it is not an XPath or XSLT tutorial. The reader should know the basics of XPath covered in Chapter 1. I also assume you know what the default XSLT processing rules are and how XSLT determines what templates should be processed next. If you feel you need some help in these areas, I would recommend either Michael Kay's XSLT Programmer's Reference (Wrox Press, 2004) or Evan Lenz' XSLT 1.0 Pocket Reference (O'Reilly, 2005). You will not see as much distinction between XSLT 1.0 and 2.0 here as in previous chapters. This is due to the fact that XSLT 1.0 was fairly complete in its ability to express most of these operations. The exception is in cases where the selection is based on constructing groups of nodes that are related by criteria other than XML's hierarchical organization. Here, the new xsl:for-each-group instruction is a highly welcome addition.

Several examples in this section use computer-science terminology commonly associated with algorithms involving tree-data structures. The uninitiated may wonder what trees have to do with XML. The answer is that XML can be viewed as a language for specifying trees and XSLT as a language that processes these trees. In fact, it is possible to make an XSLT processor process non-XML input by making the processor think it is a tree using a SAX driver. Michael Kay demonstrates this technique in XSLT Programmer's Reference (Wrox, 2001), as does Eric M. Burke in Java and XSLT (O'Reilly, 2001). In any case, the important concept that motivates the examples in this chapter is that thinking in terms of trees allows one to discover several useful XML processing techniques.

The examples provided in the chapter draw heavily on the following two test documents. To avoid repeating them in each recipe, I list them in Example 5-1 and Example 5-2.

Example 5-1. 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 5-2. 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>




XSLT Cookbook
XSLT Cookbook: Solutions and Examples for XML and XSLT Developers, 2nd Edition
ISBN: 0596009747
EAN: 2147483647
Year: 2003
Pages: 208
Authors: Sal Mangano

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net