I have a small fixed set of nodes: <a>, <b>, <c>, <d>. Each node can have a value of either 1 or 0.
Also, each node has a weight: 1, 2, 3, 4, respectively. Node attributes are not used.
How can I sum the value of each node multiplied by its weight using XSLT 1.0? Example:
<a>0</a>
<b>1</b>
<c>0</c>
<d>1</d>
Sum: 6
<a>1</a>
<b>1</b>
<c>0</c>
<d>0</d>
Sum: 3
<a>0</a>
<b>1</b>
<c>1</c>
<d>1</d>
Sum: 9
This XSLT 1.0 transformation:
when applied to the provided XML documents:
,
and
produces the wanted results, respectively:
6
3
9
Reference:
If you want to get your hands dirty with really complicated math calculations using XSLT, see this:
http://fxsl.sourceforge.net/articles/xslCalculator/The%20FXSL%20Calculator.html
XPath 2.0 / XSLT 2.0 Solution:
Use just this XPath 2.0 one-liner: