This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: How to group a list twice
- To: michael at socialchange dot net dot au
- Subject: Re: [xsl] How to group a list twice
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- Date: Mon, 28 May 2001 03:00:24 -0700 (PDT)
- Cc: xsl-list at lists dot mulberrytech dot com
- Reply-To: xsl-list at lists dot mulberrytech dot com
Michael Bauerfeld wrote:
> Hi,
>
> I'm facing a problem with a probably very simple solution, but I can't get
> it working the way I would like to.
> The main idea is to group the following xml according to <firstLevel> and
> inside of this group according to <secondLevel>.
...............................
> but I don't know how to get the 2nd grouping working. Creating a key element
> for <secondLevel> and calling the template inside of the first template
> seems not to work.
> Any ideas??
Hi Michael,
Given the following source xml document:
<response>
<record>
<firstLevel>firstname1</firstLevel>
<secondLevel>secondname11</secondLevel>
<thirdLevel>thirdname11</thirdLevel>
<secondLevel>secondname12</secondLevel>
<thirdLevel>thirdname12</thirdLevel>
<secondLevel>secondname13</secondLevel>
<thirdLevel>thirdname13</thirdLevel>
<secondLevel>secondname11</secondLevel>
<thirdLevel>thirdname11</thirdLevel>
<secondLevel>secondname12</secondLevel>
<thirdLevel>thirdname12</thirdLevel>
<secondLevel>secondname13</secondLevel>
<thirdLevel>thirdname13</thirdLevel>
<firstLevel>firstname2</firstLevel>
<secondLevel>secondname21</secondLevel>
<thirdLevel>thirdname21</thirdLevel>
<secondLevel>secondname22</secondLevel>
<thirdLevel>thirdname22</thirdLevel>
<secondLevel>secondname23</secondLevel>
<thirdLevel>thirdname23</thirdLevel>
<secondLevel>secondname21</secondLevel>
<thirdLevel>thirdname21</thirdLevel>
<secondLevel>secondname22</secondLevel>
<thirdLevel>thirdname22</thirdLevel>
<secondLevel>secondname23</secondLevel>
<thirdLevel>thirdname23</thirdLevel>
<firstLevel>firstname3</firstLevel>
<secondLevel>secondname31</secondLevel>
<thirdLevel>thirdname31</thirdLevel>
<secondLevel>secondname32</secondLevel>
<thirdLevel>thirdname32</thirdLevel>
<secondLevel>secondname33</secondLevel>
<thirdLevel>thirdname33</thirdLevel>
<secondLevel>secondname31</secondLevel>
<thirdLevel>thirdname31</thirdLevel>
<secondLevel>secondname32</secondLevel>
<thirdLevel>thirdname32</thirdLevel>
<secondLevel>secondname33</secondLevel>
<thirdLevel>thirdname33</thirdLevel>
</record>
</response>
and the following stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="NL" select="'
'"/>
<xsl:key name="kFirst" match="firstLevel" use="."/>
<xsl:key name="kSecondFirst" match="secondLevel"
use="concat(preceding-sibling::firstLevel[1], '::', .)"/>
<xsl:template match="/">
<xsl:for-each
select="/response/record/firstLevel[count(.|key('kFirst',.)[1])=1]">
<xsl:value-of select="concat('First: ', ., $NL)"/>
<xsl:for-each select="following-sibling::secondLevel
[generate-id(.) = generate-id(key('kSecondFirst',
concat(current(), '::', .))[1]
)]">
<xsl:value-of select="concat(' Second: ', ., $NL)"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The result of the transformation is:
First: firstname1
Second: secondname11
Second: secondname12
Second: secondname13
First: firstname2
Second: secondname21
Second: secondname22
Second: secondname23
First: firstname3
Second: secondname31
Second: secondname32
Second: secondname33
Hope this helped.
Cheers,
Dimitre Novatchev.
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list