I have an XML file where the number are comma-separated
<foo>
<bar val="1,23"/>
<bar val="4,56"/>
<bar val="7,89"/>
</foo>
I would like to make a sum over /foo/bar/@val
values in XSLT, but I am a bit stuck the formatting. Does anyone knows what would be the proper syntax?
I feel like I say this a lot, but it bears repeating: the whole point of XML is that it provides data in an easily parsable form. XML that contains data that can't be parsed as XML makes no sense; if at all possible, you should either fix your XML or use a different format.
Assuming the same as Dimitre, that you mean that comma is used as a decimal separator, not as a separator for list of integers.
Pure XSLT 1.0 without EXSLT node-set extension:
I am guessing, that the value specified in a
"val"
attribute is a number that has comma instead of a decimal point.Several solutions are possible:
I. XSLT 1.0
This transformation:
when applied on the originally-provided XML document:
produces the wanted result:
II. XSLT 2.0
This transformation:
when applied on the same XML document, produces the same correct result:
13.68
III. FXSL 2.x
This transformation:
when applied on the same XML document produces the same correct result:
13.68
The last solution is more flexible and can be used successfully when a more complex transformation of the values is needed before summing.