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

Kal*_*dze 6 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="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>
Run Code Online (Sandbox Code Playgroud)

dog*_*ane 3

您需要创建一个变量来保存您计算的 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>
Run Code Online (Sandbox Code Playgroud)

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

<xsl:stylesheet version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Run Code Online (Sandbox Code Playgroud)