Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT Grouping and Counting Question

Reply
Thread Tools

XSLT Grouping and Counting Question

 
 
John Galenski
Guest
Posts: n/a
 
      02-06-2009
I'm trying to group and count information based on the following XML:

<Report>
<Record>
<Id>1</Id>
<Status>
<![CDATA[ Unreviewed ]]>
</Status>
<Component>
<![CDATA[ Sales ]]>
</Component>
</Record>
<Record>
<Id>2</Id>
<Status>
<![CDATA[ To Do ]]>
</Status>
<Component>
<![CDATA[ Training ]]>
</Component>
</Record>
...
</Report>

The output would look something like this:

Unreviewed: 5
Sales: 4
Training: 1
To Do: 3
Sales: 2
Training: 1

I'm grouping ok, but cannot figure out how to count be each area. Here's a
snippet of the XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:key name="recs-by-status" match="Report/Record" use="Status" />
<xsl:template match="/">

<xsl:for-each select="Report/Record[count(. | key('recs-by-status',
Status)[1]) = 1]">
<xsl:sort select="Status" />
<xsl:value-of select="Status" /> = <xsl:value-of
select="count(key('recs-by-status', Status))"/><br />
Sales: <xsl:value-of
select="count(Report/Record[Component='Sales'][Status=key('recs-by-status',
Status)])"/><br />
Training: xsl:value-of
select="count(Report/Record[Component='Training'][Status=key('recs-by-status',
Status)])"/><br />
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

I'm trying to avoid having another for-each statement if that's at all
possible. Any pointers would be appreciated.

Thanks,
John G.


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-06-2009
John Galenski wrote:

> <xsl:for-each select="Report/Record[count(. | key('recs-by-status',
> Status)[1]) = 1]">
> <xsl:sort select="Status" />
> <xsl:value-of select="Status" /> = <xsl:value-of
> select="count(key('recs-by-status', Status))"/><br />
> Sales: <xsl:value-of
> select="count(Report/Record[Component='Sales'][Status=key('recs-by-status',
> Status)])"/><br />


Inside of the for-each a 'Record' element is the context node and that
way the
<xsl:value-of select="count(Report/Record
looks for 'Report/Record' descendants of that context 'Record' element.

I am not sure I understand your sample data correctly as you have not
provided much but don't you simply want
<xsl:value-of
select="count(key('recs-by-status',
Status)[normalize-space(Component) = 'Sales'])"/>
?
So for each group established by the key you would count those 'Record'
elements where the Component (after whitespace normalization) is 'Sales'.


> Training: xsl:value-of
> select="count(Report/Record[Component='Training'][Status=key('recs-by-status',
> Status)])"/><br />


Same here
<xsl:value-of
select="count(key('recs-by-status',
Status)[normalize-space(Component) = 'Training'])"/>

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Transforming with XSLT, Grouping elements until difference found. Jody Greening XML 5 01-06-2005 06:52 PM
Help Grouping/Counting XSLT Graham XML 3 09-17-2004 06:20 PM
XSLT XML-HTML Transformation using grouping Kevin Brown XML 3 08-28-2004 03:39 PM
XSLT: sorting and grouping Christian Ludwig XML 2 11-26-2003 10:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57