[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: grouping and counting of elements

Subject: Re: grouping and counting of elements
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Sat, 21 Apr 2007 18:36:16 +0100
Re:  grouping and counting of elements
On 4/21/07, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> 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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.