Xml 如何总结XSL中for each循环的结果?

Xml 如何总结XSL中for each循环的结果?,xml,xslt,Xml,Xslt,我是XSL新手,所以我真的不知道如何做到这一点。我有一个for-each语句,它对类型“cell”的每个元素进行一些计算。如何汇总结果并将其存储在变量中以便显示?我已经包含了代码的一部分 我希望有人知道这个问题的解决办法。谢谢你的时间和努力 <?xml version="1.0"?> <xsl:stylesheet version="1.0"> <xsl:output media-type="xml" encoding="ISO-8859-1" indent="y

我是XSL新手,所以我真的不知道如何做到这一点。我有一个for-each语句,它对类型“cell”的每个元素进行一些计算。如何汇总结果并将其存储在变量中以便显示?我已经包含了代码的一部分

我希望有人知道这个问题的解决办法。谢谢你的时间和努力

<?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 &gt; 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>

...
...
...
7
......................
0
这里我想要FlipMachineTime的总和
对于遇到的所有类型为cell的元素。
...........

您需要创建一个变量来保存已计算的FlipMachineTime节点。然后您可以对节点集求和。以下是一些示例代码:

<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>

0
要使其正常工作,请确保在样式表中包含xalan命名空间:

<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

您最好使用中的
exslt:node-set()
,而不是特定的处理器实现,因为它在各种处理器上的可移植性要高得多。在这种情况下,您需要:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:exslt="http://exslt.org/common"
            extension-element-prefixes="exslt">

 <!-- staff -->

</xsl:stylesheet>

例如,在Saxon 6.5中,您应该能够像这样使用它:

<total>
  <xsl:variable name="myTotal" select="exslt:node-set($flipMachineTimes)"/>
  <xsl:value-of select="sum($myTotal/FlipMachineTime)"/>
</total>

如果将样式表版本设置为1.1,则更容易(应该是这样):



看完你的要点后,我想你正在做一件巨大而复杂的事情,这不是一个简单的问题就能解决的。可能会有太多您可能忽略的细节。但是,如果只是显示一个值(在哪里?),我可以建议最后一件事

尝试为每个单元格创建一个变量:

  <Cell>
    <xsl:attribute name="ExternalId">
      <xsl:value-of select="@ExternalId"/>
    </xsl:attribute>
    <!-- calculated.-->
    <FlipMaschinenlaufzeit>
      <xsl:choose>
        <xsl:when test="./FlipPlanbelegungszeit &gt; 0">
          <xsl:value-of select="$planbelegungszeit - (($planbelegungszeit * ($organisatorischeAusfallzeit div 100)) + ($planbelegungszeit * ($stoerungsbedingteUnterbrechungen div 100)) + ($planbelegungszeit * ($nebenzeit div 100)))"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </FlipMaschinenlaufzeit>
  </Cell>

  <xsl:varible name="a">
     <!-- paste here the code of xsl:choose inside the cell -->
  </xsl:variable>

 <!-- create other variables for each cell in the same way (use different names, like b, c ..) -->

 <!-- after the last cell, display the sum -->
 <xsl:value-of select="$a + $b + $c + ..."/>

0

@Kaladze,大多数XSLT1.0处理器(不仅仅是Xalan)也有节点集扩展。查看处理器的文档以了解如何使用它。当然,如果您有权访问XSLT 2.0,它就变得不必要了。@Kaladze:XSLT 1.0的大多数处理器都支持EXSLT的node-set(),请参阅我的答案以了解如何使用它:)如果您提供一个完整的示例XML文档的简化问题并定义输出内容,那就太好了。@empo非常感谢您的回答。我尝试过,但在尝试显示“总计”的值时收到此错误消息。我还试图显示“myTotal”的值,但得到了相同的错误:“无法解析对变量或参数‘myTotal’的引用。变量或参数可能未定义,或者可能不在范围内。”您使用的是哪个处理器?以及..您如何/在何处定义了
$flipMachineTimes
?我正在一个名为“Process Designer”的软件应用程序中编辑此文件。我怎样才能知道我使用的是哪个处理器?如果你能展示一下你正在测试的转换,那就更有用了。如果你不能编辑文章,只需添加一个答案。
  <Cell>
    <xsl:attribute name="ExternalId">
      <xsl:value-of select="@ExternalId"/>
    </xsl:attribute>
    <!-- calculated.-->
    <FlipMaschinenlaufzeit>
      <xsl:choose>
        <xsl:when test="./FlipPlanbelegungszeit &gt; 0">
          <xsl:value-of select="$planbelegungszeit - (($planbelegungszeit * ($organisatorischeAusfallzeit div 100)) + ($planbelegungszeit * ($stoerungsbedingteUnterbrechungen div 100)) + ($planbelegungszeit * ($nebenzeit div 100)))"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </FlipMaschinenlaufzeit>
  </Cell>

  <xsl:varible name="a">
     <!-- paste here the code of xsl:choose inside the cell -->
  </xsl:variable>

 <!-- create other variables for each cell in the same way (use different names, like b, c ..) -->

 <!-- after the last cell, display the sum -->
 <xsl:value-of select="$a + $b + $c + ..."/>