Re: [xsl] Add numbers

Subject: Re: [xsl] Add numbers
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 09 Oct 2007 11:25:31 -0400
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