Skip to Content
XSLT, 2nd Edition
book

XSLT, 2nd Edition

by Doug Tidwell
June 2008
Intermediate to advanced
986 pages
27h 8m
English
O'Reilly Media, Inc.
Content preview from XSLT, 2nd Edition

The Most Common Grouping Style: group-by

As we just mentioned, everything we’ll do with grouping in XSLT 2.0 revolves around the new <xsl:for-each-group> element. This works much like <xsl:for-each>. In <xsl:for-each>, in each iteration we process an item in a sequence; with <xsl:for-each-group>, in each iteration we process a group. Thinking back to our addresses example, each group represents a unique zip code. When using <xsl:for-each-group>, the first group would be 00218, the second group would be 02718, and so forth.

To repeat from our discussion of the Muench method, we needed to do three things to group items in XSLT 1.0:

  1. Define a key for the property we want to use for grouping.

  2. Select all of the nodes we want to group. We’ll do some tricks with the key() and generate-id() functions to find the unique grouping values.

  3. For each unique grouping value, use the key() function to retrieve all nodes that match it. Because the key() function returns a node-set, we can do further sorts on the set of nodes that match any given grouping value.

With XSLT 2.0, we need to do the same basic things, but we don’t have to get bogged down with key() and generate-id(). Our tasks are as follows:

  1. Define an XPath expression for the property we want to use for grouping. All we have to do is define the XPath expression—we don’t need a key.

  2. Select all of the nodes we want to group. We select all the nodes with an XPath expression. The XSLT processor takes all the items that match this expression and groups ...

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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

XSLT Cookbook, 2nd Edition

XSLT Cookbook, 2nd Edition

Sal Mangano
Learning React, 2nd Edition

Learning React, 2nd Edition

Alex Banks, Eve Porcello

Publisher Resources

ISBN: 9780596527211Errata Page