This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Sorting and grouping
- To: xsl-list at lists dot mulberrytech dot com
- Subject: Re: [xsl] Sorting and grouping
- From: ronald heller <ronald at salience dot nl>
- Date: Tue, 30 Oct 2001 11:40:58 +0100
- Reply-To: xsl-list at lists dot mulberrytech dot com
I had a go at it, hope this suffices for you I tested in under saxon 6.4.2.
XML:
<list>
<item at="d">d</item>
<item at="e">e</item>
<item at="i">i</item>
<item at="k">k</item>
<item at="l">l</item>
<item at="a">a</item>
<item at="b">b</item>
<item at="c">c</item>
<item at="j">j</item>
<item at="f">f</item>
<item at="g">g</item>
<item at="h">h</item>
<item at="a">a</item>
<item at="b">b</item>
<item at="c">c</item>
</list>
xSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" />
<xsl:param name="group">5</xsl:param>
<xsl:template match="list">
<xsl:apply-templates select="item">
<xsl:sort select="@at" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item">
<xsl:choose>
<xsl:when test="position() mod $group =1">
<xsl:comment>Begin new group</xsl:comment>
<p><xsl:value-of select="position()"/> -
<xsl:apply-templates/></p>
</xsl:when>
<xsl:when test="position() mod $group =0">
<p><xsl:value-of select="position()"/> -
<xsl:apply-templates/></p>
<xsl:comment>End group</xsl:comment>
</xsl:when>
<xsl:when test="not(following-sibling::item) and
not(position() mod $group =0)">
<p><xsl:value-of select="position()"/> -
<xsl:apply-templates/></p>
<xsl:comment>End group</xsl:comment>
</xsl:when>
<xsl:otherwise>
<p><xsl:value-of select="position()"/> -
<xsl:apply-templates/></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Groups are separated by comments.
GRTZ
RH
At 10:33 AM 10/30/01 +0100, you wrote:
>Hi,
>can someone help me with the following problem?
>
>I have an unordered list of items:
><list>
> <item at="...">
> ...
> </item>
> ...
> <item at="...">
> ...
> </item>
></list>
>
>I want to sort this list by an 'at' attribute and then display these
>sorted items in groups.
>Each group should consist of a fixed number (let's say 5) of items. I
>can solve this problem
>only by first sorting the list into a temporary .xml file and then use
>this file for grouping.
>What's even worse, I'am not able to handle the problem in one single xsl
>transformation file.
>
>To be more specific, one can't do it in one pass like this:
> <xsl:apply-templates select="item">
> <xsl:sort select="@at" order="descending"/>
> </xsl:apply-templates>
> ....
> <xsl:template match="item[(position() mod 5) = 1]">
> <xsl:variable name="group"
>select=".|following::item[position() < 5]"/>
>because last 'select' selects following items in document order (not in
>sorted order).
>
>Is there a way to solve that without creating a temporary file?
>
>Thank you for your help.
> Zdenek
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list