Re: [xsl] Grouping by key

Subject: Re: [xsl] Grouping by key
From: gopinath.emmidisetty@xxxxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 2 Dec 2004 11:30:51 +0000
Hi,

The suggestions Geert gave for the problem worked well regarding 
grouping/categorising.  I am trying to display only the categories or 
categories and records under that category using a parameter.  I have a 
xsl:param collapse and I want to use/apply some templates based on that 
param but the <xsl:if> element doesn't seems to be working.  I want to 
know whether the usage of if element is right or I can use the element in 
this context.  I have 

Here is the xsl file and I have used <xsl:if> element at 3 places (look 
for $collapse).  Please let me know whether you need the xml file or any 
other information regarding this.  Thanks.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        version="1.0">
        <xsl:output method="xml" omit-xml-declaration="yes"/>
        <xsl:param name="catCol" select="2"/>
        <xsl:param name="indent" select="'   '"/>
        <xsl:param name="collapse" select=""/>

        <xsl:key name="total" match="*[@colno='10']" 
use="../*[@colno='2']" />
        <xsl:key name="categorise" match="account" use="*[@colno='2']" />

        <xsl:param name="columnHeaders">
                <header>CompanyName</header>
                <header>AccountNumber</header>
                <header>Town</header>
                <header>Postcode</header>
                <header>Phone</header>
        </xsl:param>

        <xsl:if test="$collapse=1">
                <xsl:template match="/">
                        <div>
                        <hr/>
                        <xsl:apply-templates select="documents" />
                        </div>
                </xsl:template>
        </xsl:if>

        <xsl:template match="documents">
                <table>
                        <xsl:apply-templates select="account[generate-id() 
= generate-id( key( 'categorise', *[@colno=$catCol] )[1] )]" 
mode="catcols">
                                <xsl:sort select="*[@colno=$catCol]"/>
                         </xsl:apply-templates>
                </table>
        </xsl:template>
 
        <xsl:template match="account" mode="catcols">
                <xsl:if test="position()=1">
                        <tr>
                                <th colspan="6" 
align="right">Total</th><th colspan="2"><xsl:value-of select="sum( //*[@colno='10'] )" /></th>
                        </tr>
                        <tr>
                                <th> <xsl:value-of 
select="msxsl:node-set($columnHeaders)/header[position()=3]" /> </th>
                                <th> <xsl:value-of 
select="msxsl:node-set($columnHeaders)/header[position()=1]" /> </th>
                                <th> <xsl:value-of 
select="msxsl:node-set($columnHeaders)/header[position()=2]" /> </th>
                                <th> <xsl:value-of 
select="msxsl:node-set($columnHeaders)/header[position()=4]" /> </th>
                                <th> <xsl:value-of 
select="msxsl:node-set($columnHeaders)/header[position()=5]" /> </th>
                        </tr>
                </xsl:if>
                <tr class="resultCategory">
                        <td colspan="3">
                                <b> <xsl:value-of select="*[@colno=$catCol]" /></b> ( <xsl:value-of select="count(.|key( 'categorise', *[@colno=$catCol] ) )" /> )
                        </td>
                        <td colspan="2">
                                <b><xsl:value-of select="sum( key( 'total', town ) )" /></b>
                        </td>

                        <xsl:if test="$collapse=1">
                                <xsl:apply-templates 
select="key('categorise', *[@colno=$catCol])" mode="catrows" />
                        </xsl:if>
                </tr>

        </xsl:template>

        <xsl:if test="$collapse=1">
                <xsl:template match="account" mode="catrows">
                        <tr>
                                <xsl:if test="position() mod 2 != 1">
                                        <xsl:attribute 
name="class">resultAlternate</xsl:attribute>
                                </xsl:if>
                        <td width="40"> <xsl:value-of select="$indent" /> 
</td>
                        <td width="100"> <xsl:value-of 
select="*[@colno=0]" /> </td>
                        <td width="100"> <xsl:value-of 
select="*[@colno=1]" /> </td>
                        </tr>
                </xsl:template>
        </xsl:if>

    </xsl:stylesheet>

Current Thread