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

Re: Add numbers

Subject: Re: Add numbers
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 09 Oct 2007 11:25:31 -0400
Re:  Add numbers
At 2007-10-09 10:04 -0500, Chaudhary, Harsh wrote:
I have an XML:
...
I need to group together the nodes which have the same key and then I
need to add the attribute "val" in all such cases.

So, The output I need is:
...
How do I go about doing this? I am using Xalan and XSLT 1.0.
...
I have used the Meunichian method to group the nodes with same keys
together.

Then you are almost there. I hope the code below helps.


. . . . . . Ken

T:\ftemp>type harsh.xml
<?xml version="1.0" encoding="UTF-8"?>
<first>
    <second>
        <a val="4" key="one">b</a>
        <a val="2" key="two">b</a>
    </second>
    <second>
        <a val="3" key="one">c</a>
    </second>
</first>

T:\ftemp>type harsh.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output indent="yes"/>

<xsl:key name="keys" match="*[@key]" use="@key"/>

<xsl:template match="/">
  <op>
    <xsl:for-each select="//*[@key]
                          [generate-id(.)=generate-id(key('keys',@key)[1])]">
      <xsl:element name="{@key}">
        <val><xsl:value-of select="sum(key('keys',@key)/@val)"/></val>
      </xsl:element>
    </xsl:for-each>
  </op>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xslt harsh.xml harsh.xsl con
<?xml version="1.0" encoding="utf-8"?>
<op>
   <one>
      <val>7</val>
   </one>
   <two>
      <val>2</val>
   </two>
</op>
T:\ftemp>


-- World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Jul'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

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.