The generate-id Function Generating Links in Unstructured Documents

page 104

5.4 Summary

In this chapter, weve examined a several ways to generate links and cross-references between different parts of a document. If your XML document has a reasonable amount of structure, you can use the id and key functions to define many different relationships between the parts of a document. Even if your XML document isnt structured, you may be able to use key and generate-id to create simple references. In the next chapter, well look at sorting and grouping, two more ways to organize the information in our XML documents. The generate-id function is not required to check if an ID it generates duplicates an ID thats already in the document. In other words, if your document has an attribute of type ID with a value of sdk3829a , theres a possibility that an ID returned by generate-id will also be sdk3829a . Its not likely, but be aware that it could happen. 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