Re: [xsl] Sort-grouping problem

Subject: Re: [xsl] Sort-grouping problem
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 17 Apr 2002 00:26:11 +0100
Hi Ragulf,

> Problem with the second stylesheet is that while the result is
> grouped by date, it is grouped by how the dates appear in document
> order, which I have no controll over, and I want it by date
> descending like in the first stylesheet.

You're currently using xsl:for-each to iterate over the unique dates;
why not sort them in the same way as you were before (remembering that
the context nodes are Date elements this time rather than News
elements)?

<xsl:template name="group-by-date">
  <xsl:variable name="unique-dates" 
    select="/NewsList/List/News
              [not(Date=preceding-sibling::News/Date)]/Date"/>
  <xsl:for-each select="$unique-dates">
    <xsl:sort select="substring(.,7,4)" data-type="number" />
    <xsl:sort select="substring(.,5,2)" data-type="number" />
    <xsl:sort select="substring(.,1,2)" data-type="number" />

    Date is: <xsl:value-of select="."/><br/>
    <xsl:for-each select="/NewsList/List/News[Date=current()]">
      <H1><xsl:value-of select="P1"/></H1>
      <!-- Other such stuff here, but I would like to put this functionality 
           into another template -->
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

If you have lots of News elements, you might find the Muenchian Method
more efficient than what you're doing at the moment (see
http://www.jenitennison.com/xslt/grouping/muenchian.html).

---

In XSLT 2.0:

<xsl:template name="group-by-date">
  <xsl:for-each-group select="/NewsList/List/News"
                      group-by="Date">
    <xsl:sort select="substring(Date,7,4)" data-type="number" />
    <xsl:sort select="substring(Date,5,2)" data-type="number" />
    <xsl:sort select="substring(Date,1,2)" data-type="number" />

    Date is: <xsl:value-of select="Date"/><br/>
    <xsl:for-each select="current-group()">
      <H1><xsl:value-of select="P1"/></H1>
      <!-- Other such stuff here, but I would like to put this functionality 
           into another template -->
    </xsl:for-each>
  </xsl:for-each-group>
</xsl:template>

Cheers,

Jeni

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


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


Current Thread