[xsl] Yes, grouping is possible even on N keys, where N is not known at compile time (Was: Re: [xsl] Muench method for two or three keys? (Sorting and Grouping))

Subject: [xsl] Yes, grouping is possible even on N keys, where N is not known at compile time (Was: Re: [xsl] Muench method for two or three keys? (Sorting and Grouping))
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 17 Jan 2005 19:33:11 +1100
On Sun, 16 Jan 2005 13:19:32 +0100, Joris Gillis <roac@xxxxxxxxxx> wrote:
> Tempore 05:00:33, die 01/16/2005 AD, hinc in
> xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Daniel O'Donnell
> <daniel.odonnell@xxxxxxxx>:
> 
> > So what I need is a sheet that does the following:
> >   a) sorts records by head
> >   b) groups records so that if two subfield1's are children of heads
> > with the same content they are put in the same record
> >   c) repeats the same for subfield2's
> >
> 
> Hi,
> 
> I don't think you can use muenchian grouping for more than 1 level of
> grouping (could be wrong).

Yes, Muenchian grouping using more than one key (nested grouping) is
being used in practice.


See the example pointed to by Jeni's classic tutorial on grouping -- a
search will find quite a sufficient number of other such examples on
this list, on the web and in various newsgroups:

       http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html

Even recently there was an example of recursive grouping with
undefined (statically) number of keys, which can be used for a nested
grouping of any depth:

      http://www.biglist.com/lists/xsl-list/archives/200412/msg01032.html


There's certainly space for improvement in the efficiency of this last
one, though.

Hope this helped.


Cheers.

Dimitre Novatchev.


> The following method starts with the muenchian method, but uses another
> algorithm for deeper hierarchy levels (the subfields). The stylesheet is
> not memory-friendly but it does output what you want:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0">
> <xsl:output method="xml" indent="yes"/>
> <xsl:key name="headkey" match="record" use="head"/>
> <xsl:key name="sub1" match="subfield1" use="../head"/>
> <xsl:key name="sub2" match="subfield2" use="../subfield1"/>
> 
> <xsl:template match="index">
>   <index>
>     <xsl:apply-templates
> select="record[generate-id(.)=generate-id(key('headkey', head)[1])]/head">
>                <xsl:sort select="."/>
>        </xsl:apply-templates>
>   </index>
> </xsl:template>
> 
> <xsl:template match="head">
>   <item><xsl:value-of select="."/>
>        <xsl:if test="count(key('sub1', .)[text()])&gt;0">
>                <list>
>                        <xsl:apply-templates select="key('sub1',
> .)[not(../preceding::record[head=current()]/subfield1=.)]">
>                                <xsl:sort select="."/>
>                        </xsl:apply-templates>
>                </list>
>        </xsl:if>
>   </item>
> </xsl:template>
> 
> <xsl:template match="subfield1[text()]">
>   <item><xsl:value-of select="."/>
>        <xsl:if test="count(key('sub2', .)[text()])&gt;0">
>                <list>
>                        <xsl:apply-templates select="key('sub2',
> .)[not(../preceding::record[subfield1=current()]/subfield2=.)]">
>                                <xsl:sort select="."/>
>                        </xsl:apply-templates>
>                </list>
>        </xsl:if>
>        <xsl:if test="count(key('sub2', .)[text()])=0">
>                <xsl:apply-templates select="../locater"/>
>        </xsl:if>
>   </item>
> </xsl:template>
> 
> <xsl:template match="subfield2[text()]">
>   <item><xsl:value-of select="."/>
>                <xsl:apply-templates select="../locater"/>
>   </item>
> </xsl:template>
> 
> <xsl:template match="locater">
>     <xsl:if test="string-length()!=0">
>                <xsl:text>, </xsl:text>
>                <seg type="locater">
>                        <xsl:value-of select="."/>
>                </seg>
>     </xsl:if>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> regards,
> --
> Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
> Vincit omnia simplicitas
> Keep it simple

Current Thread