I want to sum all the values that meet my condition (see below). In case the condition is not met, I want to return 0. Please see my below code and advise. Currently it is returning empty .
Condition : sum all values if fieldlabel = 'A', if not retuen (in this example need to return 30) Let s suppose i am looking for 'C'instead of 'A', in this case i have to return 0
Example file :
<root>
<parent>
<element>
<elementname>A</elementname>
<elementvalue>10</elementvalue>
</element>
<element>
<elementname>B</elementname>
<elementvalue>20</elementvalue>
</element>
<element>
<elementname>A</elementname>
<elementvalue>30</elementvalue>
</element>
</parent>
</root>
Code :
<xsl:template match="/">
<xsl:for-each select="/root/parent/element">
<xsl:variable name="field">
<xsl:value-of select="elementname" />
</xsl:variable>
<xsl:if test="$field='A'">
<xsl:value-of select="sum(elementvalue)" />
</xsl:if>
</xsl:for-each>
<xsl:variable name="finalresult">
<xsl:choose>
<xsl:when test="$fieldname = '' ">0</xsl:when>
<xsl:otherwise><xsl:value-of select="$field"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>