[Home] [By Thread] [By Date] [Recent Entries]
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. 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>
|

Cart



