Re: [xsl] Sorting under for-each-group

Subject: Re: [xsl] Sorting under for-each-group
From: "andrew welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 8 Aug 2006 15:03:17 +0100
On 8/8/06, Mark Wilson <drmark@xxxxxxxxxxxxxxx> wrote:
The Stylesheet below does most of what I want -- It groups all of the
entries for a single Subject under a single subject heading. I would like
subsort under each group (Subject) first by author then by title, and if
possible, when there is/are no author/authors, by title alone. The example
would put the Kolar book first, then the Siegar Book, both under the single
heading "Catalogs. Austria".

I have spent a lot of time on it but have not been able to solve the sort
within groupings problem. Can someone help?
stylesheet and xml input sample below


<xsl:stylesheet xmlns:xsl="";
<xsl:template match="/">
        <xsl:for-each-group select="/List/Entry" group-by="Subject">
            <xsl:copy-of select="Subject"/>
            <xsl:for-each select="current-group()">
                <xsl:copy-of select="*[not(self::Subject)]"/>

- <List>
    - <Entry>
          <Subject>Catalogs. Austria</Subject>
          <Author>Sieger, Hermann Walter</Author>
        - <Book>
              <Title>Zeppelinpost spezial-katalog</Title>
    -  </Entry>
    - <Entry>
      <Subject>Catalogs. Austria</Subject>
      <Author>Kolar, Hugo</Author>
    - <Book>
          <Title>Katalog znamok Rakzsko 1850-1918</Title>

Add a couple of xsl:sort's to your for-each:

<xsl:for-each select="current-group()">
 <xsl:sort select="Author"/>
 <xsl:sort select="Book/Title"/>

This will sort by Author first, then by Book/Title.  If no Author is
present it will just sort by Book/Title.


Current Thread