Summary Creating Links and Cross-References

page 105

Chapter 6. Sorting and Grouping Elements

By now, I hope youre convinced that you can use XSLT to convert big piles of XML data into other useful things. Our examples to this point have pretty much gone through the XML source in whats referred to as document order. Wed like to go through our XML documents in a couple of other common ways, though: • We could sort some or all of the XML elements, then generate output based on the sorted elements. • We could group the data, selecting all elements that have some property in common, then sorting the groups of elements. Well give several examples of these operations in this chapter.

6.1 Sorting Data with xsl:sort

The simplest way to rearrange our XML elements is to use the xsl:sort element. This element temporarily rearranges a collection of elements based on criteria we define in our stylesheet.

6.1.1 Our First Example

For our first example, well have a set of U.S. postal addresses that we want to sort. No chauvinism is intended here; obviously every country has different conventions for mailing addresses. We just needed a short sample document that can be sorted in many useful ways. Heres our original document: ?xml version=1.0? addressbook address name titleMr.title first-nameChester Hasbrouckfirst-name last-nameFrisbylast-name name street1234 Main Streetstreet citySheboygancity stateWIstate zip48392zip address address name first-nameMaryfirst-name last-nameBackstaygelast-name name street283 First Avenuestreet citySkunk Havencity stateMAstate zip02718zip address address name titleMs.title first-nameNataliefirst-name last-nameAttiredlast-name name street707 Breitling Waystreet cityWinter Harborcity stateMEstate zip00218zip address page 106 address name first-nameHarryfirst-name last-nameBackstaygelast-name name street283 First Avenuestreet citySkunk Havencity stateMAstate zip02718zip address address name first-nameMaryfirst-name last-nameMcGoonlast-name name street103 Bryant Streetstreet cityBoylstoncity stateVAstate zip27318zip address address name titleMs.title first-nameAmandafirst-name last-nameReckonwithlast-name name street930-A Chestnut Streetstreet cityLynncity stateMAstate zip02930zip address addressbook Wed like to generate a list of these addresses, sorted by last-name . Well use the magical xsl:sort element to do the work. Our stylesheet looks like this: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xsl:output method=text indent=no xsl:strip-space elements= xsl:variable name=newline xsl:text xsl:text xsl:variable xsl:template match= xsl:for-each select=addressbookaddress xsl:sort select=namelast-name xsl:value-of select=nametitle xsl:text xsl:text xsl:value-of select=namefirst-name xsl:text xsl:text xsl:value-of select=namelast-name xsl:value-of select=newline xsl:value-of select=street xsl:value-of select=newline xsl:value-of select=city xsl:text, xsl:text xsl:value-of select=state xsl:text xsl:text xsl:value-of select=zip xsl:value-of select=newline xsl:value-of select=newline xsl:for-each xsl:template xsl:stylesheet