XSLT:树片段的总和将始终返回 0 !
我遇到了一个看似愚蠢的问题,但我找不到解决方案......使用 XLST,我需要对模板计算的值列表进行求和。 因此,我将这些值存储在树结构中(根元素“Numbers”中包含的“Number”元素列表)。 但无论我尝试用这个自制列表做什么,它都会返回任何内容、0 或错误...
有人知道我做错了什么吗?
<!-- create the tree fragment -->
<xsl:variable name="_subTotals">
<Numbers>
<xsl:for-each select="List">
<xsl:variable name="_Size">
<xsl:call-template name="GetSize">
<xsl:with-param name="_value" select="@value"/>
</xsl:call-template>
</xsl:variable>
<Number>
<xsl:value-of select="$_Size"/>
</Number>
</xsl:for-each>
</Numbers>
</xsl:variable>
<!-- this returns an error: expression must result into a node-set -->
<xsl:message terminate="no">
<xsl:value-of select="sum($_subTotals/Numbers/Number)"/>
</xsl:message>
<!-- transform the tree fragment into a node-set
<xsl:variable name="_Total" select="msxsl:node-set($_subTotals)"/>
<!-- returns nothing -->
<xsl:for-each select="$_Total/Numbers/Number">
<xsl:message terminate="no">
<xsl:value-of select="@value"/>
</xsl:message>
</xsl:for-each>
<!-- returns 0 -->
<xsl:value-of select="sum($_Total/Numbers/Number)"/>
I am stuck with a problem which seems stupid but I cannot find out the solution... With XLST, I need to sum a list of values calculated by a template. So I stored these values in a tree structure (a list of "Number" elements contained in a root element "Numbers"). But whatever I try to do with this self-made list, it will return either nothing, 0 or an error...
Does someone know what I am doing wrong ?
<!-- create the tree fragment -->
<xsl:variable name="_subTotals">
<Numbers>
<xsl:for-each select="List">
<xsl:variable name="_Size">
<xsl:call-template name="GetSize">
<xsl:with-param name="_value" select="@value"/>
</xsl:call-template>
</xsl:variable>
<Number>
<xsl:value-of select="$_Size"/>
</Number>
</xsl:for-each>
</Numbers>
</xsl:variable>
<!-- this returns an error: expression must result into a node-set -->
<xsl:message terminate="no">
<xsl:value-of select="sum($_subTotals/Numbers/Number)"/>
</xsl:message>
<!-- transform the tree fragment into a node-set
<xsl:variable name="_Total" select="msxsl:node-set($_subTotals)"/>
<!-- returns nothing -->
<xsl:for-each select="$_Total/Numbers/Number">
<xsl:message terminate="no">
<xsl:value-of select="@value"/>
</xsl:message>
</xsl:for-each>
<!-- returns 0 -->
<xsl:value-of select="sum($_Total/Numbers/Number)"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种快速求和动态生成的值的方法(可能您正在调用的模板没有产生预期的结果?如果是这样,您必须提出另一个问题并提供代码和 XML 文档没有这些,任何人都无法提供帮助,猜测也没有任何用处。):
当上述转换应用于任何 XML 文档(忽略)时,就会产生所需的结果:
Here is a quick way how to sum dynamically-generated values (Probably the template you are calling is not producing the expected results? If so, you must ask another question and provide both the code and the XML document on which the code operates. Without these no one can help and guesses would not be useful.):
When the above transformation is applied on any XML document (ignored), the wanted result is produced:
递归是函数式语言中通常的答案。 类似于:
如果不了解更多的 GetSize 和输入文档,就很难更具体地了解正确的 XPath 来设置递归调用的上下文节点。
Recursion is the usual answer in functional languages. Something like:
Without knowing more of GetSize and the input document it is hard to be more specific about the right XPath to set the context node for the recursive call.
就我而言,我遇到了完全相同的问题,即 sum($something/element) 返回 0 并且 sum($something/*) 返回正确的值。
我的问题是由以下 UBL Invoice 样式表声明引起的:
xmlns="urn:oasis:names:specation:ubl:schema:xsd:Invoice-2"
删除此行后,sum($something/element) 返回了正确的值。
In my case I had exactly the same problem that sum($something/element) returned 0 and sum($something/*) returned the correct value.
My problem was caused by the following UBL Invoice stylesheet declaration:
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
After removing this line, sum($something/element) returned the correct value.