如何总结 XSL 中 for-each 循环的结果?
我是 XSL 新手,所以我真的不知道该怎么做。我有一个 for-each 语句,它对“单元格”类型的每个元素进行一些计算。如何汇总结果并将其存储在变量中以便显示它?我已经包含了部分代码。
我希望有人知道这个问题的解决方案。感谢您的时间和努力!
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个变量来保存您计算的 FlipMachineTime 节点。然后您可以对节点集求和。以下是一些示例代码:
要使其正常工作,请确保在样式表中包含 xalan 命名空间:
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:
To get this to work, make sure you include the xalan namespace in your stylesheet:
您最好使用 http://exslt.org/common< 中的
exslt:node-set()
/a> 比特定处理器实现 EXSLT 在各种处理器上更可移植。在这种情况下,您需要:例如在 Saxon 6.5 中,您应该能够像这样使用它:
如果将样式表版本设置为 1.1,那就更容易了(应该如此):
在看到您的要点后,我认为你正在研究一些巨大而复杂的东西,不能用一个简单的问题来解决。您可能遗漏了太多细节。但是,如果只是显示值(在哪里???)的情况,我可以建议最后一件事。
尝试为每个单元格创建一个变量:
You should better use
exslt:node-set()
from http://exslt.org/common than the specific processor implementation being EXSLT much more portable on the various processors. In that case, you need:For example in Saxon 6.5 you should be able to use it like:
If you set the stylesheet version to 1.1, that's easier (as it should be):
After seeing your gist, I think you are working on something huge and complex which can not be solved here with a simple question. There can be too many details that you might have omitted. However if it's just the case of displaying a value (where???) I can suggest this last thing.
Try to create a variable for each cell: