Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Getting nested-group item counts

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 8/8/2009 1:42:00 PM
Chris Gannon wrote:
> I've got some xslt that's performing nested grouping and everything is
> almost right with the world.
> 
> I'm grouping first on Location, then on Category.
> 
> What I would like to do is display the item count next to each
> Category for that Location but I keep getting the total count of all
> items with the same Category name...
> 
> What I'd like to see is...
>   Location: East  Category: Meetings [4]
>    East meeting item 1
>    East meeting item 2
>    East meeting item 3
>    East meeting item 4
> 
>   Location: West Category: Meetings [2]
>    West meeting item 1
>    West meeting item 2
> 
> What I'm getting is
>   Location: East  Category: Meetings [6]
>    East meeting item 1
>    East meeting item 2
>    East meeting item 3
>    East meeting item 4
> 
>   Location: West Category: Meetings [6]
>    West meeting item 1
>    West meeting item 2

Here is a sample stylesheet, once you have used two keys where the 
second key concatenates the Location and Category values you can simply 
count the number of items the key function returns:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

   <xsl:output method="text"/>

   <xsl:key name="k1" match="Row" use="@Location"/>
   <xsl:key name="k2" match="Row" use="concat(@Location, '|', @Category)"/>

   <xsl:template match="/">
     <xsl:for-each select="dsQueryResponse/Rows/Row[generate-id() = 
generate-id(key('k1', @Location)[1])]">
       <xsl:for-each select="key('k1', @Location)[generate-id() = 
generate-id(key('k2', concat(@Location, '|', @Category))[1])]">
         <xsl:variable name="group" select="key('k2', concat(@Location, 
'|', @Category))"/>
         <xsl:value-of select="concat('Location: ', @Location, ' 
Category: ', @Category, ' [', count($group), ']&#10;')"/>
         <xsl:for-each select="$group">
           <xsl:value-of select="concat('&#9;', @Item, '&#10;')"/>
         </xsl:for-each>
       </xsl:for-each>
     </xsl:for-each>
   </xsl:template>

</xsl:stylesheet>
-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent