2

次の関数を使用して、この列の価格 * 数量 + 合計 (請求書ごとおよびクライアントごと) を計算するクライアント請求書があります。

<xsl:template name="sumProducts"> 
    <xsl:param name="pList"/> 
    <xsl:param name="pRunningTotal" select="0"/>         
    <xsl:choose> 
       <xsl:when test="$pList"> 
           <xsl:variable name="varMapPath" select="$pList[1]"/> 
           <xsl:call-template name="sumProducts"> 
               <xsl:with-param name="pList" select="$pList[position() > 1]"/> 
                   <xsl:with-param name="pRunningTotal" select="$pRunningTotal + $varMapPath/unitprice *  $varMapPath/quantity"/>                    
           </xsl:call-template> 
       </xsl:when> 
       <xsl:otherwise> 
           $<xsl:value-of select="format-number($pRunningTotal, '#,##0.00')"/> 
       </xsl:otherwise> 
    </xsl:choose> 
</xsl:template>

クライアントごとのすべての請求書の合計と、すべてのクライアントの合計請求書を計算したいと思います。

ありがとうございました

4

1 に答える 1

0

再帰テンプレートを使用する代わりに、変数を使用します。各アイテムの小計を計算し、それをアイテムに追加するこの例を見てください。次に、合計を計算するために sum() 関数を使用します。

入力 XML:

<root>
  <items>
    <item price="19" quantity="12" />
    <item price="5" quantity="3" />
    <item price="4" quantity="1" />
    <item price="2" quantity="2" />
  </items>
</root>

Xslt:

<?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="xml" indent="yes"/>

  <xsl:template match="items">
    <root>
    <xsl:variable name="items-with-sub-totals">
      <xsl:for-each select="item">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:attribute name="sub-total">
            <xsl:value-of select="@price * @quantity"/>
          </xsl:attribute>
        </xsl:copy>
      </xsl:for-each>
    </xsl:variable>

    <debug>
      <xsl:copy-of select="msxsl:node-set($items-with-sub-totals)/*"/>
    </debug>

      <xsl:value-of select="concat('Total:', sum(msxsl:node-set($items-with-sub-totals)/item/@sub-total))" />
    </root>

  </xsl:template>

</xsl:stylesheet>

出力 (変数がどのように見えるかを示すためにデバッグ情報を追加しました):

<?xml version="1.0" encoding="utf-8"?>
<root>
  <debug>
    <item price="19" quantity="12" sub-total="228" />
    <item price="5" quantity="3" sub-total="15" />
    <item price="4" quantity="1" sub-total="4" />
    <item price="2" quantity="2" sub-total="4" />
  </debug>
  Total:251
</root>

これはあなたが試みているものとは少し異なるアプローチですが、うまくいけばうまくいくでしょう

于 2012-10-12T17:30:08.077 に答える