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

问题描述

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

解决方法

        您需要创建一个变量来保存已计算的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>
为了使它起作用,请确保在样式表中包括xalan命名空间:
<xsl:stylesheet version=\"1.0\" xmlns:xalan=\"http://xml.apache.org/xalan\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
    ,        您最好使用http://exslt.org/common中的
exslt:node-set()
,而不要使用EXSLT在各种处理器上具有更大可移植性的特定处理器实现。在这种情况下,您需要:
<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,则更容易(应该如此):
 <total>
   <xsl:value-of select=\"sum(exslt:node-set($flipMachineTimes/FlipMachineTime))\"/>
 </total>
看了您的要点之后,我认为您正在研究巨大而又复杂的问题,而这在这里无法通过一个简单的问题来解决。您可能已经忽略了太多的细节。但是,如果只是显示值的情况(在哪里???),我可以建议这最后一件事。 尝试为每个单元格创建一个变量:
  <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 + ...\"/>
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...