xslt - Need help transforming xml via xlst -
hi need sum element values in xml via xlst 1.0 , give output.
this xml i'm working on
<store> <counter> <item> <id>1</id> <name>desk</name> <price>96</price> </item> <item> <id>1</id> <name>chair</name> <price>323</price> </item> <item> <id>1</id> <name>lamp</name> <price>52</price> </item> <item> <id>2</id> <name>desk</name> <price>200</price> </item> <item> <id>2</id> <name>chair</name> <price>62</price> </item> <item> <id>3</id> <name>desk</name> <price>540</price> </item> <item> <id>3</id> <name>desk</name> <price>235</price> </item> <item> <id>3</id> <name>desk</name> <price>455</price> </item> <item> <id>4</id> <name>desk</name> <price>370</price> </item> </counter> </store>
and desired output be:
<name>desk</name> <sum> "sum of desk prices </sum> <name>chair</name> <sum> "sum of chair prices </sum> <name>lamp</name> <sum> "sum of lamp prices </sum>
whatever did fare, either gave me 000 sum elements or output errors. if point me in right direction appreciate much. thank in advance.
please find xslt helps achieve requirement
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output indent="yes" method="xml"/> <xsl:key name="lang" match="item" use="name"/> <xsl:template match="/"> <output> <xsl:for-each select="//item[count(. | key('lang', name)[1]) = 1]"> <name> <xsl:value-of select="name"/> </name> <sum> <xsl:value-of select="sum(key('lang', name)//price)"/> </sum> </xsl:for-each> </output> </xsl:template> </xsl:stylesheet>
output of above xslt:
<output> <name>desk</name> <sum>1896</sum> <name>chair</name> <sum>385</sum> <name>lamp</name> <sum>52</sum> </output>
Comments
Post a Comment