xsl递归嵌套调用乘法的和

时间:2013-04-23 16:58:13

标签: xslt recursion report reporting

我遇到了与此链接相似的问题 XSL - sum multiplication of elements

我的问题是,我有订单而不仅仅是商品,每个订单都有一件或多件商品。

最后我需要的是所有订单的成本,(除了必须将每个成本乘以数量) 我已经解决了xsl 2,但报告只支持1.0

<root>
    <order>
      <item type="goods">
        <quantity unit="pcs">1</quantity>
        <cost>2.89</cost>
      </item>
      <item type="goods">
        <quantity unit="pcs">10</quantity>
        <cost>210.25</cost>
      </item>
    </order>
    <order>
      <item type="goods">
        <quantity unit="pcs">1</quantity>
        <cost>4.15</cost>
      </item>
    </order>
    <order>
      <item type="goods">
        <quantity unit="pcs">5</quantity>
        <cost>1.25</cost>
      </item>
      <item type="goods">
        <quantity unit="pcs">20</quantity>
        <cost>189.63</cost>
      </item>     
      <item type="goods">
        <quantity unit="pcs">3</quantity>
        <cost>1</cost>
      </item>     
      <item type="goods">
        <quantity unit="pcs">9</quantity>
        <cost>6</cost>
      </item>     
    </order>    
</root>

1 个答案:

答案 0 :(得分:0)

如果你只想要总计,那么这应该这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:call-template name="GetGrandTotal">
      <xsl:with-param name="items" select="//item" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="GetGrandTotal">
    <xsl:param name="items" />
    <xsl:param name="total" select="0" />

    <xsl:choose>
      <xsl:when test="not($items)">
        <xsl:value-of select="format-number($total, '0.00')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="GetGrandTotal">
          <xsl:with-param name="items" select="$items[position() > 1]" />
          <xsl:with-param name="total"
                          select="$total + ($items[1]/quantity * $items[1]/cost)" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

在样本输入上运行时,结果为:

5965.39