I want to add up the values in this input XML at COMS level for every CDS/CD node. The catch is I want the sum to be done only for tags having ID's 'A' or 'B'. The value under the tag 'C' should not be computed in the sum.
<?xml version="1.0"?>
<root>
<CDS>
<CD>
<COMS>
<COMP>
<ID>A</ID>
<VAL>1.220</VAL>
</COMP>
<COMP>
<ID>B</ID>
<VAL>2.220</VAL>
</COMP>
<COMP>
<ID>C</ID>
<VAL>5.220</VAL>
</COMP>
</COMS>
</CD>
</CDS>
<CDS>
<CD>
<COMS>
<COMP>
<ID>A</ID>
<VAL>5.2</VAL>
</COMP>
<COMP>
<ID>B</ID>
<VAL>6.5</VAL>
</COMP>
<COMP>
<ID>C</ID>
<VAL>10.5</VAL>
</COMP>
</COMS>
</CD>
</CDS>
<CDS>
<CD>
<COMS>
<COMP>
<ID>A</ID>
<VAL>4.2</VAL>
</COMP>
<COMP>
<ID>B</ID>
<VAL>5.8</VAL>
</COMP>
<COMP>
<ID>C</ID>
<VAL>14.8</VAL>
</COMP>
</COMS>
</CD>
</CDS>
Output XML:
<CDS>
<CD>
<COMS>
<COMP>
<SUM_VALUE>3.44</SUM_VALUE>
</COMP>
</COMS>
</CD>
<CD>
<COMS>
<COMP>
<SUM_VALUE>11.7</SUM_VALUE>
</COMP>
</COMS>
</CD>
<CD>
<COMS>
<COMP>
<SUM_VALUE>10</SUM_VALUE>
</COMP>
</COMS>
</CD>
</CDS>
My XSLT code only tries to sum all the values and gives the final output. It would be helpful if someone can explain how to sum the xml values in the for:each loop for the respective comp levels.
I was initially trying with this line of code but it adds up all the values
No comments:
Post a Comment