XSLT Grouping and Counting Question

J

John Galenski

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 xmlns:xsl="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.
 
M

Martin Honnen

John said:
<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'])"/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top