This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Grouping question
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Wed, 22 May 2002 10:21:24 +0100
- Subject: Re: [xsl] Grouping question
- Organization: Jeni Tennison Consulting Ltd
- References: <B8BE783DF30DD51182DC00508BF330EE02929D06@us-bv-m03.bv.tek.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Bryan Schnabel wrote:
> While we wait for Jeni to give us the well-thought-out XSLT 1.0
> Meunchian method, here's some fun to consider with XSLT 2.0.
>
> Assuming your input is well formed (not </color>):
> <data>
> <n color="R">1</n>
> <n color="R">2</n>
> <n color="R">3</n>
> <n color="Y">4</n>
> <n color="Y">5</n>
> <n color="Y">6</n>
> <n color="W">7</n>
> <n color="W">8</n>
> <n color="W">9</n>
> </data>
>
> Something like this would work:
>
> <xsl:template match="data">
> <tables>
> <xsl:for-each-group select="n" group-by="@color">
> <xsl:sort select="." />
> <table>
> <xsl:for-each select="current-group()">
> <xsl:if test="position() mod 2">
> <xsl:variable name="color" select="@color" />
> <tr>
> <td>
> <xsl:value-of select="." />
> </td>
> <xsl:if test="following-sibling::n[@color=$color]">
> <td>
> <xsl:value-of select="following-sibling::n[@color=$color]" />
> </td>
> </xsl:if>
> </tr>
> </xsl:if>
> </xsl:for-each>
> </table>
> </xsl:for-each-group>
> </tables>
> </xsl:template>
You could do the second level of grouping with xsl:for-each-group as
well. I *think* that this is the way to do it...
<xsl:template match="data">
<tables>
<xsl:for-each-group select="n" group-by="@color">
<xsl:sort select="." />
<table>
<xsl:for-each-group select="current-group()"
group-adjacent="ceiling(position() div 2)">
<tr>
<td>
<xsl:value-of select="current-group()[1]" />
</td>
<td>
<xsl:value-of select="current-group()[2]" />
</td>
</tr>
</xsl:for-each-group>
</table>
</xsl:for-each-group>
</tables>
</xsl:template>
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list