Re: [xsl] Meunchian Method - Grouping with attributes

Subject: Re: [xsl] Meunchian Method - Grouping with attributes
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 26 Jan 2002 00:41:51 +0000
Hi Brian,

> I've been trying to set the key as <xsl:key name="domain4tech"
> match="technology/@domain" use="technology"/> but this has proved
> useless. Any suggestions?

I think that the reason you're running into difficulties is because
this is actually a two-level grouping problem. The first level of
grouping is grouping all the technology elements by their value (e.g.
tech1, tech2, tech3). The second level of grouping is only required to
get rid of the duplicates - you need to group the technology elements
for each particular technology by their domain attribute.

In XSLT 2.0 terms, the grouping would look like:

  <xsl:for-each-group select="product/technology"
                      group-by=".">
    <xsl:sort select="." />
    <xsl:value-of select="." />
    <xsl:for-each-group select="current-group()"
                        group-by="@domain">
      domain = <xsl:value-of select="@domain" />
    </xsl:for-each-group>
  </xsl:for-each-group>

or, alternatively:

  <xsl:for-each-group select="product/technology"
                      group-by=".">
    <xsl:sort select="." />
    <xsl:value-of select="." />
    <xsl:for-each select="distinct-values(current-group()/@domain)">
      domain = <xsl:value-of select="@domain" />
    </xsl:for-each>
  </xsl:for-each-group>

That's probably not much use to you (unless you're using Saxon 7.0),
but it does help see the overall structure of what we're doing.

To do two levels of grouping with keys, you need two keys - one for
the first level, one for the second level. The first level key is easy
- you're grouping technology elements by their value:

<xsl:key name="tech" match="technology" use="." />

The second level is a little harder, because you need to index the
technology elements by *both* their technology and their domain. You
can create a key to do this by concatenating the two values by which
you want to group together, as follows:

<xsl:key name="tech-by-domain" match="technology"
         use="concat(., '+', @domain)" />

Then you need the old Muenchian trick to get the unique values, and
Bob's your uncle:

  <xsl:for-each select="product/technology
                          [generate-id() =
                           generate-id(key('tech', .)[1])]">
    <xsl:sort select="." />
    <xsl:value-of select="." />
    <xsl:for-each
      select="key('tech', .)
                [generate-id() =
                 generate-id(key('tech-by-domain',
                                 concat(., '+', @domain))[1])]">
      domain = <xsl:value-of select="@domain" />
    </xsl:for-each>
  </xsl:for-each>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread