[xsl] Problem with grouping multi level nodes

Subject: [xsl] Problem with grouping multi level nodes
From: "Nguyen, Mike (Contractor)" <Mike.Nguyen@xxxxxxxxxxxxx>
Date: Tue, 16 Mar 2004 16:17:57 -0500
Hi,

I have the following problem with grouping and wonder if anyone can tell me
what I am doing wrong: 

Sample xml:

<students>
   <student>
      <name>Student 1</name>    
      <classes>            
         <class>            
            <title>soccer</title>        
            <category>sport</category>                   
         </class>            
         <class>            
            <title>football</title>        
            <category>sport</category>                   
         </class>            
      </classes>            
   </student>
   <student>
      <name>Student 2</name>    
      <classes>            
         <class>            
            <title>soccer</title>        
            <category>sport</category>                   
         </class>            
      </classes>            
   </student>
</students>


Expected output:

Student group by title

soccer
   Student 1   
   Student 2

football
   Student 1

Student group by category

sport
   Student 1
   Student 2

Student group by title and category

soccer - sport
   Student 1
   Student 2

football - sport
   Student 1


Current xsl:

<xsl:key name="titles" match="class" use="title"/>
<xsl:key name="categories" match="class" use="category"/> <xsl:key
name="titles-categories" match="class" use="concat(title,'-',category)"/>

<xsl:template match="/">
   <xsl:apply-templates/>
</xsl:template">

<xsl:template match="/students">

   <p>Student group by Title</p>
   <xsl:for-each select="/title[not(. = preceding::title)]">
      <p><xsl:value-of select="."/></p>

      <xsl:for-each select="key('titles',.)">
         <p><xsl:value-of select="../../name"/></p>
      </xsl:for-each>

   </xsl:for-each>

   <p>Student group by Category</p>
   <xsl:for-each select="/category[not(. = preceding::category)]">
      <p><xsl:value-of select="."/></p>

      <xsl:for-each select="key('categories',.)">
         <p><xsl:value-of select="../../name"/></p>
      </xsl:for-each>

   </xsl:for-each>

   <p>Student group by Title-Category</p>
   <xsl:for-each select="/class[count(. | key('titles-categories',
concat(title,'-',category))[1]) =1]">
      <p><xsl:value-of select="title"/> - <xsl:value-of
select="category"/></p>

      <xsl:for-each select="key('titles-categories',
concat(title,'-',category))">
         <p><xsl:value-of select="../../name"/></p>
      </xsl:for-each>

   </xsl:for-each>

</xsl:template">   


Output using the above xsl:


Student group by title

soccer
   Student 1   
   Student 2

football
   Student 1

Student group by category

sport
   Student 1
   Student 1
   Student 2

Student group by title and category

soccer - sport
   Student 1
   Student 2

football - sport
   Student 1


As you can see from the output above, I got a duplicate Student 1 in sport
category.  I am not sure how to eliminate this duplicate row.  Sorry for the
long email and thanks in advance for any helps.  


Mike

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread