[xsl] two level grouping

Subject: [xsl] two level grouping
From: "Martina Kinzl" <Martina.Kinzl@xxxxxxxxxx>
Date: Thu, 12 Aug 2004 12:10:51 +0200
Hi there,

I am trying to create an index of keywords of my online-book.
My source XML-file contains a lot of keywords which are more specified with
Elements (Level1 or even Level2):

<Stichwortliste>
<Stichwortdetails>
	<Stichwort>
		<Level1>ius civile</Level1>
		<Titel titel_id="CBAHDGEI">B|rgerliches Recht, Zivilrecht,
ius civile</Titel>
		<Kapitel>kap1_1.xml</Kapitel>
	</Stichwort>
	<Stichwort>
		<Level1>Privatrecht</Level1>
		<Titel titel_id="CBAHDGEI">B|rgerliches Recht, Zivilrecht,
ius civile</Titel>
		<Kapitel>kap1_1.xml</Kapitel>
	</Stichwort>
	<Stichwort>
		<Level1>Privatrecht</Level1>
		<Level2>allgemeines</Level2>
		<Titel titel_id="CBAHDGEI"> Recht im objektiven und
subjektiven Sinn</Titel>
		<Kapitel>kap2_4.xml</Kapitel>
	</Stichwort>
	<Stichwort>
		<Level1>Privatrecht</Level1>
		<Titel titel_id="CBAHDGEI"> Das Gesamtrechtssystem </Titel>
		<Kapitel>kap5_3.xml</Kapitel>
	</Stichwort>
</Stichwortdetails>
<Stichwortliste>


I would like to have the following output:

The keywords are sorted alphabetically and if there are more keywords that
are equal the name of the keyword should be printed once and the
chapter-specifications should follow. And if there are keywords that own the
more specified Level2 then these keywords (+chapter-specifications) should
not be given out until all the keywords (only with Level1-elements of the
same name) have been printed out.

Here is what I have:

I
ius civile [kap1_1]

P
Privatrecht [kap1_1],
	allgemeines [kap2_4], [kap5_3]


Here is what I would like to have:

I
Ius civile [kap1_1]

P
Privatrecht [kap1_1], [kap5_3]
	allgemeines [kap2_4]


Here is my XSL:

<xsl:key name="keyword-initial" match="Stichwort"
use="translate(substring(Level1,1,1),'abcdefghijklmnopqrstuvwxyzdv|123456789
0@','ABCDEFGHIJKLMNOPQRSTUVWXYZDV\###########')"/>
<xsl:key name="contacts-by-surname" match="Stichwort" use="Level1"/>
<xsl:template match="Stichwortdetails">

	<xsl:for-each select="Stichwort[count(. | key('keyword-initial',
translate(substring(Level1,1,1), 'abcdefghijklmnopqrstuvwxyzdv|1234567890@',
'ABCDEFGHIJKLMNOPQRSTUVWXYZDV\###########'))[1]) = 1]">
		<xsl:sort select="Level1"/>
		<xsl:variable name="initial"
select="translate(substring(Level1,1,1),
'abcdefghijklmnopqrstuvwxyzdv|1234567890@',
'ABCDEFGHIJKLMNOPQRSTUVWXYZDV\###########')"/>
		<a name="{$initial}"/>
			<div><xsl:value-of select="$initial"/></div>
			<ul>
				<xsl:for-each select="key('keyword-initial',
$initial)">
					<xsl:sort select="Level1"/>
					<xsl:if test="count(. |
key('contacts-by-surname', Level1)[1]) = 1">
						<br/>
						<span><xsl:value-of
select="Level1"/></span>
					</xsl:if>
                    				    <xsl:if
test="not(name(Level1/following-sibling::*) = 'Level2')">
					            	<span
style="color:green;"><xsl:apply-templates select="Level1"
mode="index.link"/></span>
                    				    </xsl:if>
				    <xsl:for-each select="Level2">
						<xsl:sort select="Level2"/>
						<br/>
						<span style="color:red;
margin-left:20px;"><xsl:apply-templates select="."
mode="index.link"/></span>
					</xsl:for-each>
				</xsl:for-each>
			</ul>
			<br/>
	</xsl:for-each>
</xsl:template>


It seems that the second for-each-loop <xsl:for-each select="Level2"> isnt
correct, but I dont know how to do it right.
It must be something like a grouping within a grouping?!?
Can you give me any advice?

Thanks
Martina

Current Thread