Add up elements value by matching name element
-
Hi - Any help or guidance would be appreciated. I am trying a new xml file with the total of one of the specific elements - below is the original xml file:
<Report>
<Unit>
<Name>t1</Name>
<Price>32</Price>
<Other>sdasd</Other>
</Unit>
<Unit>
<Name>t1</Name>
<Price>42</Price>
<Other>dasdasd</Other>
</Unit>
<Unit>
<Name>t2</Name>
<Price>11</Price>
<Other>asdas</Other>
</Unit>
<Unit>
<Name>t3</Name>
<Price>21</Price>
<Other>dasd</Other>
</Unit>
<Unit>
<Name>t1</Name>
<Price>65</Price>
<Other>dasda</Other>
</Unit>The xsl that I am trying to us is as follows: instead of giving me 1 value it gives me a list of all t1 values rather than a sumation
<test>
<xsl:if test="Name = 't1'">
<xsl:variable name="value1" select ="sum(Price)"/>
<xsl:value-of select="$value1"/>
</xsl:if>
</test>thanks
-
Hi - Any help or guidance would be appreciated. I am trying a new xml file with the total of one of the specific elements - below is the original xml file:
<Report>
<Unit>
<Name>t1</Name>
<Price>32</Price>
<Other>sdasd</Other>
</Unit>
<Unit>
<Name>t1</Name>
<Price>42</Price>
<Other>dasdasd</Other>
</Unit>
<Unit>
<Name>t2</Name>
<Price>11</Price>
<Other>asdas</Other>
</Unit>
<Unit>
<Name>t3</Name>
<Price>21</Price>
<Other>dasd</Other>
</Unit>
<Unit>
<Name>t1</Name>
<Price>65</Price>
<Other>dasda</Other>
</Unit>The xsl that I am trying to us is as follows: instead of giving me 1 value it gives me a list of all t1 values rather than a sumation
<test>
<xsl:if test="Name = 't1'">
<xsl:variable name="value1" select ="sum(Price)"/>
<xsl:value-of select="$value1"/>
</xsl:if>
</test>thanks
This XSL stylesheet will output a single 'test' tag containing the total you want. Note that I construct a list of all the 'Price' tags (with
//Price
) and filter that list with a predicate on the sibling Name tag ([../Name='t1']
).<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
<test>
<xsl:value-of select="sum(//Price[../Name='t1'])"/>
</test>
</xsl:template></xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p