加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:37:58 所属栏目:百科 来源:网络整理
导读:我是XSL的新手,所以我真的不知道怎么做.我有一个for-each语句,它为类型“cell”的每个元素进行一些计算.如何总结结果并将它们存储在变量中,以便我可以显示它?我已经包含了部分代码. 我希望有人知道这个问题的解决方案.感谢您的时间和精力! ?xml version="1
我是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">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读