Skip to Content
XSLT
book

XSLT

by Doug Tidwell
August 2001
Intermediate to advanced
480 pages
11h 16m
English
O'Reilly Media, Inc.
Content preview from XSLT

More Sophisticated Techniques

Up to now, we’ve written a simple XML document that contains references to other XML documents, then we created a stylesheet that combines all those referenced XML documents into a single output document. That’s all well and good, but we’ll probably want to do more advanced things. For example, it might be useful to generate a document that lists all items ordered by all the customers. It might be useful to sort all the purchase orders by the state to which they were shipped, by the last name of the customer, or to group them by the state to which they were shipped. We’ll go through some of these scenarios to illustrate the design challenges we face when generating documents from multiple input files.

The document() Function and Sorting

Our first challenge will be to generate a listing of all purchase orders and sort them by state. This isn’t terribly difficult; we’ll simply use the <xsl:sort> element in conjunction with the document() function. Here’s the heart of our new stylesheet:

<body>
  <h3>Selected Purchase Orders - <i>Sorted by state</i></h3>
  <xsl:for-each 
  select="document(/report/po/@filename)/purchase-order/customer/address/state">
    <xsl:sort select="."/>
    <xsl:apply-templates select="ancestor::purchase-order"/>
  </xsl:for-each>
</body>

What makes this process slightly challenging is the fact that we’re sorting on one thing (the value of the <state> element), then invoking <xsl:apply-templates> against the <purchase-order> ancestor of the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XSLT

Learning XSLT

Michael Fitzgerald
Inside XSLT

Inside XSLT

Steven Holzner
XSLT Cookbook

XSLT Cookbook

Sal Mangano

Publisher Resources

ISBN: 0596000537Supplemental ContentCatalog PageErrata