Re: [xsl] Muench method for two or three keys? (Sorting and Grouping)

Subject: Re: [xsl] Muench method for two or three keys? (Sorting and Grouping)
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Sun, 16 Jan 2005 13:19:32 +0100
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).
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