[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to group a list twice

Subject: Re: How to group a list twice
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 28 May 2001 03:00:24 -0700 (PDT)
grouping xml twice
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.