xsl-list
[Top] [All Lists]

Re: [xsl] grouping and counting of elements

2007-04-21 10:36:36
On 4/21/07, Mukul Gandhi <gandhi(_dot_)mukul(_at_)gmail(_dot_)com> wrote:
Hi Leonid,
  Below is a XSLT 2.0 solution for this:

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

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <Totals>
      <xsl:for-each select="All_Results/Result[1]/*">
        <xsl:variable name="name" select="name()" />
        <xsl:element name="{$name}">
          <xsl:for-each-group select="../../Result/*[name() = $name]"
group-by=".">
            <xsl:if test="not(normalize-space(.) = '')">
              <Tag value="{.}" count="{count(current-group())}" />
            </xsl:if>
          </xsl:for-each-group>
        </xsl:element>
      </xsl:for-each>
    </Totals>
  </xsl:template>

</xsl:stylesheet>

Heres another way which doesn't rely on all elements being present in
the first <Result>:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
        <Totals>
                <xsl:for-each-group
select="/All_Results/Result/*[normalize-space()]" group-by="name()">
                        <xsl:element name="{current-grouping-key()}">
                                <xsl:for-each-group select="current-group()" 
group-by=".">
                                        <Tag value="{current-grouping-key()}" 
count="{count(current-group())}"/>
                                </xsl:for-each-group>
                        </xsl:element>
                </xsl:for-each-group>
        </Totals>
</xsl:template>
</xsl:stylesheet>

cheers
andrew

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>