Sum of Multiplied Values

前端 未结 2 488
夕颜
夕颜 2021-01-23 02:09

I have a fairly convoluted XML file and I need to do a weighted average of a few values within it using XSL. I am able to complete a sum of the weights OR of the values, but I c

2条回答
  •  粉色の甜心
    2021-01-23 02:32

    For completeness, if you want to sum over a computed quantity in XSLT 1.0, there are three ways of doing it:

    (a) recursion: write a recursive template that processes the items in the sequence one by one, computing the total as it goes.

    (b) create an XML tree in which the computed quantities are node values, and then process this tree using the sum() function. To do this in a single stylesheet you will need the exslt:node-set() extension function.

    (c) use an extension function provided by the XSLT vendor, or user-written using the facilities provided by the vendor for calling external functions.

    In XSLT 2.0, it can always be done using the construct

    sum(for $x in node-set return f($x))
    

    where f is a function that computes the quantity.

提交回复
热议问题