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

发布于 2024-11-10 06:36:08 字数 1482 浏览 1 评论 0原文

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

我希望有人知道这个问题的解决方案。感谢您的时间和努力!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2024-11-17 06:36:08

您需要创建一个变量来保存您计算的 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">

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">
水中月 2024-11-17 06:36:08

您最好使用 http://exslt.org/common< 中的 exslt:node-set() /a> 比特定处理器实现 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 > 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 + ..."/>

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 > 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 + ..."/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文