XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Stu PlumSubject: Grouping/Counting Problem
Author: Stu Plum
Date: 25 Oct 2006 07:27 AM
Hi Guys,

I think this one is an easy one to fix, but I can't get my head around it so any help would be greatly appreciated.
From the sample xml below I want to count the items that have a matching itematt attribute. But at the same time I don't want to have to specifiy the itematt as a predicate, as these are generated dynamically.

For example the results I would expect to see (not formatted) from the sample xml would be:
AAA : 3
BBB : 1
CCC : 3
DDD : 3
empty : 12

Sample XML
<root>
<items>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="DDD">more xml</item>
<item itematt="DDD">more xml</item>
<item itematt="DDD">more xml</item>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="AAA">more xml</item>
<item itematt="AAA">more xml</item>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="CCC">more xml</item>
<item itematt="CCC">more xml</item>
<item itematt="CCC">more xml</item>
<item itematt="">more xml</item>
<item itematt="BBB">more xml</item>
<item itematt="">more xml</item>
<item itematt="">more xml</item>
<item itematt="AAA">more xml</item>
</items>
</root>

Postnext
Ivan PedruzziSubject: Grouping/Counting Problem
Author: Ivan Pedruzzi
Date: 25 Oct 2006 10:38 AM

Use xsl:key to group by @itematt; search this forum you will find many posts on how to do grouping in XSLT


Ivan Pedruzzi
Stylus Studio Team
http://www.stylusstudio.com/xml_download.html

Postnext
Stu PlumSubject: Grouping/Counting Problem
Author: Stu Plum
Date: 25 Oct 2006 12:06 PM
This is where i'm upto so far:

<xsl:key name="itemAttsKey" match="/root/items/item/@itematt" use="."/>

<xsl:for-each select="/root/items/item/@itematt[generate-id()=generate-id(key('itemAttsKey',.))]">
<li><xsl:value-of select="../@itematt" />:<span><xsl:value-of select="count(../@itematt)" /></span></li>
</xsl:for-each>

This produces a list of unique @itematt's, but the count for each one only displays '1', I don't know how to count() each itematt from within the for-each loop. any more pointers??

Posttop
Ivan PedruzziSubject: Grouping/Counting Problem
Author: Ivan Pedruzzi
Date: 25 Oct 2006 12:25 PM
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="itemAttsKey" match="/root/items/item" use="@itematt"/>
<xsl:template match="/">
<xsl:for-each select="/root/items/item/[count(. | key('itemAttsKey',@itematt)[1] ) = 1]">
<li>
<xsl:value-of select="@itematt"/>:<span><xsl:value-of select="count(key('itemAttsKey',@itematt))"/></span></li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Hope this helps
Ivan Pedruzzi
Stylus Studio Team
http://www.stylusstudio.com/xml_download.html

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.