[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Grouping & use of keys

Subject: RE: Grouping & use of keys
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 26 Feb 2009 18:33:44 -0000
RE:  Grouping & use of keys
> I am a newbie at XSLT, have only written 1 identity transform 
> that took me 2 weeks to get working. I have a grouping task 
> for some xml data 

Grouping is MUCH easier in XSLT 2.0. Do you have anything that constrains
you to XSLT 1.0?

> 
> I think of <xsl:key......> as a declaration, similar to "new" 
> in some OO languages. 

Think of xsl:key as "create index", so

<xsl:key name="es" match="employee" use="surname"/>

creates an index of employees by surname, and

key('es', 'Smith')

finds the employees with surname 'Smith'

In XSLT 2.0, to group employees by surname, you simply do

<xsl:for-each-group select="employee" group-by="surname">
  then
  <xsl:for-each select="current-group()">

In XSLT 1.0 you use Muenchian grouping, which logically does this:

for each employee
  get the surname
  call key() to find all the employees with that surname
  apply [1] to get the first of these employees
  test whether this one is the current employee
  if so, treat this employee as the "owner" of the group
     find all employees with this surname by calling key() again
     and process them as required

OK?

Michael Kay
http://www.saxonica.com/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.