Getting Unique values and adding the values in XSLT -
hi pretty new xslt need on simple xsl code.
my input xml
<?xml version="1.0" encoding="ascii"?> <node name="person" received="1" good="1" bad="0" condition="byperson:1111"> </node> <node name="person" received="1" good="1" bad="0" condition="byperson:1111"> </node> <node name="person" received="1" good="1" bad="0" condition="byperson:2222"> </node> <node name="person" received="1" good="1" bad="0" condition="byperson:2222"> </node> <node name="person" received="1" good="1" bad="0" condition="byperson:3333"> </node> and expecting result sum of received , , bad need added once per unique condition.
something this
<?xml version="1.0" encoding="ascii"?> <received>3</received > <good>3</good> <bad>0</bad> i trying below code no success far getting sum of everything, sum on each 'condition' once.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:template match="/"> <xsl:value-of select= "sum(node@received)"/> <xsl:value-of select= "sum(node/@good)"/> <xsl:value-of select= "sum(node/@bad)"/> </xsl:template>
the following stylesheet uses xsl:key group <node> elements value of @condition. using meunchien method key() , generate-id(), select first node element each unique @condition , generate sum() of attributes of selected node elements.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output indent="yes"/> <xsl:key name="nodesbycondition" match="node" use="@condition"/> <xsl:template match="/"> <results> <xsl:variable name="distinctnodes" select="*/node[generate-id() = generate-id(key('nodesbycondition', @condition)[1])]"/> <received> <xsl:value-of select= "sum($distinctnodes/@received)"/> </received> <good><xsl:value-of select= "sum($distinctnodes/@good)"/></good> <bad><xsl:value-of select= "sum($distinctnodes/@bad)"/></bad> </results> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment