当前位置 : 主页 > 网页制作 > xml >

xml – 如何在for-each循环中对此变量求和?

来源:互联网 收集:自由互联 发布时间:2021-06-13
如何获得for-each循环中所有项目的总成本? ?xml version="1.0" encoding="utf-8" ?Characters Character ID="1" Name="Simmo" Inventory MaxSlots="20" Item ID="1" Name="Gold" Cost="1" Count100/Count /Item Item ID="1" Name="hat" Cost
如何获得for-each循环中所有项目的总成本?

<?xml version="1.0" encoding="utf-8" ?>
<Characters>
  <Character ID="1" Name="Simmo">
    <Inventory MaxSlots="20">
      <Item ID="1" Name="Gold" Cost="1">
        <Count>100</Count>
      </Item>
      <Item ID="1" Name="hat" Cost="10">
        <Count>1</Count>
      </Item>
      <Item ID="2" Name="stick" Cost="15">
        <Count>2</Count>
      </Item>
    </Inventory>
</Character>
</Characters>

例如1 * 100 10 * 1 15 * 2 = 140

我未完成的xsl解决方案:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>xsl file</title>
      </head>
      <body>
        <ul>
          <xsl:for-each select="/Characters/Character">
            <li>
              Character name: <xsl:value-of select="@Name"/>
              <br/>
              <xsl:for-each select="Inventory/Item">
                <xsl:variable name="cos" select="@Cost"/>
                <xsl:variable name="cou" select="Count"/>
                <xsl:variable name="kor" select="$cos*$cou"/>
                Total cost:<xsl:value-of select="$kor"/>
                <br/>
              </xsl:for-each>
              <br />
            </li>
          </xsl:for-each>
        </ul>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

当前输出:

人物名称:Simmo
总费用:100
总费用:10
总费用:30

但如何获得角色名称:Simmo总费用:140

三种可能性:

(1)转到XSLT 2.0并编写

Character name:  <xsl:value-of select="@Name"/>
<br/>
Total cost:  <xsl:value-of select="
  sum(for $i in Inventory/Item return $i/@Cost * $i/Count)
"/>

(2)在XSLT 1.0中,使用已经指向的related question中详细描述的EXSLT节点集扩展(a)创建一组包含@Cost和Count的乘积的节点,以及(b)求和它们.

(3)编写递归命名模板以循环遍历项目集,计算运行总计.这看起来像这样(未经测试):

<xsl:template match="/">
  ... some of your code omitted here ...
  <xsl:for-each select="/Characters/Character">
    <li>
      Character name: <xsl:value-of select="@Name"/>
      <br/>
      <xsl:call-template name="sum-items">
        <xsl:with-param name="item"
          select="Inventory/Item[1]"/>
        <xsl:with-param name="accumulator"
          select="0"/>
      </xsl:call-template>
      <br />
    </li>
  </xsl:for-each>
  ...
</xsl:template>

<xsl:template name="sum-items">
  <xsl:param name="item"/>
  <xsl:param name="accumulator"/>

  <xsl:choose>
    <xsl:when test="not($item)">
      <!--* done, return result *-->
      Total cost:   <xsl:value-of select="$accumulator"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="acc" 
        select="$accumulator + ($item/@Cost * $item/Count)"/>
      <xsl:call-template name="sum-items">
        <xsl:with-param name="item"
          select="$item/following-sibling::item[1]"/>
        <xsl:with-param name="accumulator" select="$acc"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:when>
</xsl:template>
网友评论