XSLT Grouping and Counting Question

Discussion in 'XML' started by John Galenski, Feb 6, 2009.

  1. 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.
     
    John Galenski, Feb 6, 2009
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Feb 6, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Ludwig

    XSLT: sorting and grouping

    Christian Ludwig, Nov 24, 2003, in forum: XML
    Replies:
    2
    Views:
    628
    Christian Ludwig
    Nov 26, 2003
  2. Kevin Brown
    Replies:
    3
    Views:
    582
    Kevin Brown
    Aug 28, 2004
  3. Graham

    Help Grouping/Counting XSLT

    Graham, Sep 17, 2004, in forum: XML
    Replies:
    3
    Views:
    589
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Sep 17, 2004
  4. Jody Greening
    Replies:
    5
    Views:
    690
    Jody Greening
    Jan 6, 2005
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    241
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page