xsl-list
[Top] [All Lists]

Re: [xsl] Add numbers

2007-10-09 08:27:05
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="*[(_at_)key]" use="@key"/>

<xsl:template match="/">
  <op>
    <xsl:for-each select="//*[(_at_)key]
                          [generate-id(.)=generate-id(key('keys',@key)[1])]">
      <xsl:element name="{(_at_)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(_at_)CraneSoftwrights(_dot_)com
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


--~------------------------------------------------------------------
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>