相乘值的总和 [英] Sum of Multiplied Values
问题描述
我有一个相当复杂的 XML 文件,我需要使用 XSL 对其中的几个值进行加权平均.我能够完成权重或值的总和,但我无法使乘法起作用.我收到一个错误:
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 cannot get the multiplication to work. I get an error:
XPTY0004:不允许有多个项目的序列作为第一个'*' 的操作数
XPTY0004: A sequence of more than one item is not allowed as the first operand of '*'
我无法共享 XML,但我已将 XML 简化为以下示例(假设有大量 foo):
I am not able to share the XML, but I have simplified the XML to the following example (assume there are a large number of foos):
<group>
<fooList>
<foo>
<attributeList>
<Attribute ID="1" Weight="0.5">
<otherParams />
</Attribute>
</attributeList>
<Properties>
<PhysicalProperties>
<Volume Average="125" Unknown="50" />
</PhysicalProperties>
</Properties>
</foo>
</fooList>
</group>
我目前获得加权平均值的尝试如下:
My current attempt to get the weighted average is the following:
<xsl:variable name="WeightedVolume" select="sum(/group/fooList/foo[attributeList/Attribute/[@ID=$test_id]]/attributeList/Attribute/@Weight * /group/fooList/foo[attributeList/Attribute/[@ID=$test_id]]/Properties/PhysicalProperties/Volume/@Average)"/>
我知道有类似的问题可用 - 但他们中的大多数都处理诸如 foo 求和和乘法之类的问题
I know there are similar questions available - but most of them deal with something like summing and multiplying foo
<foo>
<Weight>0.5</Weight>
<VolumeAverage>125</VolumeAverage>
</foo>
关于这个StackOverflow 问题的答案对我很有吸引力,但我不能似乎使它起作用.
The answer on this StackOverflow Question appeals to me, but I cannot seem to make it work.
我使用 Saxonica 的 Saxon-HE 9.5.1.1N 和 Visual Studio 2013.
I'm using Saxon-HE 9.5.1.1N from Saxonica, with Visual Studio 2013.
已编辑我能够得到一些适用于 XSL 2 的东西,但需要有一个适用于 XSL1 的后备.
Edited I was able to get something to work for XSL 2, but need to have a fall-back for XSL1.
<xsl:variable name="WeightedVolume" select="sum(for $i in /group/FooList/foo[attributeList/Attribute[@ID=$test_id] return $i/AttributeList/Attribute/@Weight * $i/Properties/PhysicalProperties/Volume/@Average)"/>
推荐答案
要遵循您链接到的那个问题中的示例,您可以在 XSLT 2.0/XPath 2.0 中使用它:
To follow the example in that question you linked to, you would use this in XSLT 2.0/XPath 2.0:
<xsl:variable name="FoosToCalculate"
select="/group/fooList/foo[attributeList/Attribute/@ID = $test_id]" />
<xsl:variable name="WeightedVolume"
select="sum($FoosToCalculate/(attributeList/Attribute/@Weight *
Properties/PhysicalProperties/Volume/@Average)
)"/>
在 XSLT 1.0 中进行这种求和要复杂得多,通常涉及使用递归模板或 node-set()
函数的某种表现形式.下面是后者的一个例子:
Doing this summing in XSLT 1.0 is considerably more involved and typically involves either using recursive templates or some manifestation of the node-set()
function. Here is an example of the latter:
<xsl:stylesheet version="1.0"
xmlns:ex="http://exslt.org/common"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<!-- determine $test_id however you need to -->
<xsl:variable name="products">
<xsl:for-each
select="/group/fooList/foo[attributeList/Attribute/@ID = $test_id]">
<product>
<xsl:value-of select="attributeList/Attribute/@Weight *
Properties/PhysicalProperties/Volume/@Average" />
</product>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(ex:node-set($products)/product)"/>
</xsl:template>
</xsl:stylesheet>
这篇关于相乘值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!