How can I sum-up the results of a for-each loop in

2019-04-28 10:19发布

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

标签: xml xslt
2条回答
ら.Afraid
2楼-- · 2019-04-28 10:34

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:

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

For example in Saxon 6.5 you should be able to use it like:

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

If you set the stylesheet version to 1.1, that's easier (as it should be):

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

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:

  <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 + ..."/>
查看更多
迷人小祖宗
3楼-- · 2019-04-28 10:39

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>

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">
查看更多
登录 后发表回答