RE: [xsl] Yet another grouping question

Subject: RE: [xsl] Yet another grouping question
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Thu, 16 Sep 2004 10:04:41 +0100
An XSLT 2.0 solution:

<xsl:template match="/">
  <index>
    <xsl:for-each-group select="indexRoot/indexItem"
group-by="@primary">
      <primary name="{@primary}">
          <xsl:for-each select="current-group()[@secondary]">
            <secondary name="{@secondary}"/>
          </xsl:for-each>
      </primary>
    </xsl:for-each-group>
  </index>
</xsl:template>

No keys or generate-id() in sight :)

cheers
andrew

> So I've gone through the 'XSLT Questions and Answers' and
> Jeni's site and it is still not clear to me why my Muenchien
> grouping isn't working. Specificaly, the step related to
> limiting responses to the first in the group.
>
> Imagine I have an XML file like this:
>
> <indexRoot>
>   <indexItem primary="ide" secondary="object repository"/>
>   <indexItem primary="ide" secondary="Project Manager"/>
>   <indexItem primary="ide" secondary="Code Editor"/>
>   <indexItem primary="projects" secondary="type of"/>
>   <indexItem primary="projects" secondary="additional projects"/>
>   <indexItem primary="unmanaged code"/>
> </indexRoot>
>
>
> I would like to group it something like:
>
>
>       <index>
>            <primary name="ide">
>                  <secondary name="object repository"/>
>                  <secondary name="Project Manager"/>
>                  <secondary name="Code Editor"/>
>            </primary>
>            <primary name="projects">
>                  <secondary name="type of"/>
>                  <secondary name="additional projects"/>
>            </primary>
>            <primary name="unmanaged code"/>
>       </index>
>
> I have defined a key like this:
>
>  <xsl:key name="index-by-primary" match="indexItem"
> use="@secondary" />
>
> When I try to get the first with something like:
>
>  <xsl:for-each select="indexItem[count(. | key('index-by-primary',
> @primary)[1]) = 1]">
>     <xsl:message>
>        <xsl:value-of select="@primary"/>, <xsl:value-of
> select="@secondary"/>
>     </xsl:message>
>   </xsl:for-each>
>
> I get a message for every indexItem, not just the first of
> each unique @secondary.

Current Thread