[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: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 21 Apr 2007 22:04:08 +0530
Re:  grouping and counting of elements
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>

When the above stylesheet is applied to XML:

<All_Results>
 <Result>
    <Name>John</Name>
    <Country>UK</Country>
    <!-- other upto 100 elements -->
    <Color>Red</Color>
 </Result>
 <Result>
    <Name>John</Name>
    <Country>US</Country>
    <!-- other upto 100 elements -->
    <Color>Green</Color>
 </Result>
 <Result>
    <Name>Thomas</Name>
    <Country>Estonia</Country>
    <!-- other upto 100 elements -->
    <Color>
    </Color>
 </Result>
 <Result>
    <Name>
    </Name>
    <Country>UK</Country>
    <!-- other upto 100 elements -->
    <Color>Red</Color>
 </Result>
</All_Results>

The output produced is:

<?xml version="1.0" encoding="UTF-8"?>
<Totals>
  <Name>
     <Tag value="John" count="2"/>
     <Tag value="Thomas" count="1"/>
  </Name>
  <Country>
     <Tag value="UK" count="2"/>
     <Tag value="US" count="1"/>
     <Tag value="Estonia" count="1"/>
  </Country>
  <Color>
     <Tag value="Red" count="2"/>
     <Tag value="Green" count="1"/>
  </Color>
</Totals>

On 4/20/07, Leonid Lyublinski <llyublin@xxxxxxxxx> wrote:
Hi experts,
I have seen similar questions to mine, yet didn't find a good mach.
Please point to the right thread if exists.
I have a source Xml:


<All_Results> <Result> <Name>John</Name> <Country>UK</Country> <!-- other upto 100 elements --> <Color>Red</Color> </Result> <Result> <Name>John</Name> <Country>US</Country> <!-- other upto 100 elements --> <Color>Green</Color> </Result> <Result> <Name>Thomas</Name> <Country>Estonia</Country> <!-- other upto 100 elements --> <Color> </Color> </Result> <Result> <Name> </Name> <Country>UK</Country> <!-- other upto 100 elements --> <Color>Red</Color> </Result> </All_Results>

Each <Result> has the same list of sub-elements, some might not have a
text value

I want to aggregate and get something like this:

<Totals>
  <Name>
     <Tag value="John" count="2" />
     <Tag value="Thomas" count="1" />
  </Name>
  <Country>
     <Tag value="UK" count="2" />
     <Tag value="US" count="1" />
     <Tag value="Estonia" count="1" />
  </Country>
  <Color>
     <Tag value="Red" count="2" />
     <Tag value="Green" count="1" />
  </Color>
<!-- other elements grouped by element name, sorted by total of
element values-->
</Totals>

Please advice.
Thanks,
Leonid


--
Regards,
Mukul Gandhi

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.