如何总结 XSL 中 for-each 循环的结果? [英] How can I sum-up the results of a for-each loop in XSL?
问题描述
我是 XSL 的新手,所以我真的不知道该怎么做.我有一个 for-each 语句,它对单元格"类型的每个元素进行一些计算.我如何总结结果并将它们存储在一个变量中以便我可以显示它?我已经包含了一部分代码.
I am new to XSL so I don´t really know how to do this. I have a for-each statement that does some computations for each element of a type "cell". How can I sum-up the results and store them in a variable so I can display it? I have included a part of the code.
我希望有人知道这个问题的解决方案.感谢您的时间和精力!
I hope someone knows the solution to this problem. Thank you for your time and effort!
<?xml version="1.0"?>
<xsl:stylesheet version="1.0">
<xsl:output media-type="xml" encoding="ISO-8859-1" indent="yes"/>
<xsl:key name="object-map" match="/Data/Objects/*" use="@ExternalId"/>
<xsl:template match="/">
<Data>
<Objects>
...
...
...
<xsl:for-each select="Data/Objects/Cell">
<xsl:attribute name="PpXmlVer">
<xsl:text>7.0</xsl:text>
</xsl:attribute>
......................
<!--Calculating Machine Time: -->
<Cell>
<xsl:attribute name="ExternalId">
<xsl:value-of select="@ExternalId"/>
</xsl:attribute>
<!-- calculated.-->
<FlipMachineTime>
<xsl:choose>
<xsl:when test="./FlipPlanedOccupationTime > 0">
<xsl:value-of select="here is a complicated formula to compute"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</FlipMaschineTime>
</Cell>
</xsl:for-each>
Here I would like to have the sum of FlipMachineTime
for all encountered elements of type cell.
...........
</Objects>
</Data>
推荐答案
您需要创建一个变量来保存您计算的 FlipMachineTime 节点.然后你可以总结节点集.下面是一些示例代码:
You need to create a variable to hold the FlipMachineTime nodes you have computed. Then you can sum the nodeset. Here is some sample code:
<xsl:variable name="flipMachineTimes">
<xsl:for-each select="/Data/Objects/Cell">
<FlipMachineTime>
<xsl:choose>
<xsl:when test="./FlipPlanedOccupationTime > 0">
<xsl:value-of select="here is a complicated formula to compute"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</FlipMachineTime>
</xsl:for-each>
</xsl:variable>
<total>
<xsl:variable name="myTotal" select="xalan:nodeset($flipMachineTimes)"/>
<xsl:value-of select="sum($myTotal/FlipMachineTime)"/>
</total>
要使其正常工作,请确保在样式表中包含 xalan 命名空间:
To get this to work, make sure you include the xalan namespace in your stylesheet:
<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
这篇关于如何总结 XSL 中 for-each 循环的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!