2016-11-26 9 views
1

私はXSLTに問題があります。ノードの合計をパラメータで計算するのが好きです。XSLTパラメータのノード数の合計

XMLソースは次のようになります。

<Documents> 
    <Document> 
    <Deleted>0</Deleted> 
    <DocumentType>2</DocumentType> 
    <Currency>EUR</Currency> 
    <CurrencyRate>4.368400</CurrencyRate> 
    <GrossValue>1000.00</GrossValue> 
    <DeliveryDate>2016-08-01</DeliveryDate> 
    <FormOfPayment>2</FormOfPayment> 
    <DueDate>2016-09-28</DueDate> 
    </Document> 
    <Document> 
    <Deleted>0</Deleted> 
    <DocumentType>2</DocumentType> 
    <Currency>EUR</Currency> 
    <CurrencyRate>4.368400</CurrencyRate> 
    <GrossValue>2000.00</GrossValue> 
    <DeliveryDate>2016-08-05</DeliveryDate> 
    <FormOfPayment>5</FormOfPayment> 
    <DueDate>2016-09-05</DueDate> 
    </Document> 
    <Document> 
    <Deleted>0</Deleted> 
    <DocumentType>2</DocumentType> 
    <Currency>EUR</Currency> 
    <CurrencyRate>4.368400</CurrencyRate> 
    <GrossValue>3000.00</GrossValue> 
    <DeliveryDate>2016-08-30</DeliveryDate> 
    <FormOfPayment>2</FormOfPayment> 
    <DueDate>2016-10-29</DueDate> 
    </Document> 
    <Document> 
    <Deleted>0</Deleted> 
    <DocumentType>2</DocumentType> 
    <Currency>EUR</Currency> 
    <CurrencyRate>4.368400</CurrencyRate> 
    <GrossValue>2500.00</GrossValue> 
    <DeliveryDate>2016-08-26</DeliveryDate> 
    <FormOfPayment>5</FormOfPayment> 
    <DueDate>2016-09-10</DueDate> 
    </Document> 
</Documents> 

私は必要な結果をパラメータFormOfPaymentによってGrossValue額の合計です。

  • のでFormOfPaymentが5であれば、変数GrossValueCardは4500
  • する必要があり、FormOfPaymentが2であるとき、変数GrossValueCashは4000

である必要があり、私はそのようにこれを実行しようとした。

<xsl:template name="Suma"> 
    <xsl:param name="index" select="1"/> 
    <xsl:param name="nodes"/> 
    <xsl:param name="totalAmount" select="0"/> 
    <xsl:variable name="currentAmount" select="translate($nodes[$index],',','.')"/> 
    <xsl:choose> 
     <xsl:when test="count($nodes)=0"> 
      <xsl:value-of select="0"/> 
     </xsl:when> 
     <xsl:when test="$index=count($nodes)"> 
      <xsl:value-of select="$totalAmount + $currentAmount"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:call-template name="Suma"> 
       <xsl:with-param name="index" select="$index+1"/> 
       <xsl:with-param name="totalAmount" select="$totalAmount + $currentAmount"/> 
       <xsl:with-param name="nodes" select="$nodes"/> 
      </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

となりますが、合計はすべてGrossValueノードです。 FormOfPaymentで合計を選択するためのパラメータを設定する必要があるというアドバイスを教えてください。

"when"または "if"の条件でこれを選択しようとしましたが、動作しませんでした。

+0

あなたは 'GrossValueCard'のためのあなたの望ましい結果の数式を提供することができます? – zx485

+0

結果GrossValueCardは、FormOfPayment5を持つドキュメントのGrossValueノードをすべて合計したものです.Xsltはすべてのドキュメントを調べ、GrossValueをGrossValueCardという名前の変数に集計します。 – jeffers

答えて

0

ノードGrossValueを選択するには、GrossValue[../FormOfPayment = 'x']のような述語を使用します。
これらの選択した値を追加するには、XSLT 1.0関数sum(...)をこの述語式で使用します。

スタイルシートは次のようになります。

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

<xsl:template match="text()" /> 

<xsl:template match="/Documents"> 
    <xsl:variable name="GrossValueCard2"> 
    <xsl:value-of select="sum(Document/GrossValue[../FormOfPayment = '2']/text())" /> 
    </xsl:variable> 
    <xsl:variable name="GrossValueCard5"> 
    <xsl:value-of select="sum(Document/GrossValue[../FormOfPayment = '5']/text())" /> 
    </xsl:variable> 
    Sum of 'FormOfPayment 2's: <xsl:value-of select="$GrossValueCard2" /> 
    Sum of 'FormOfPayment 5's: <xsl:value-of select="$GrossValueCard5" /> 
</xsl:template> 

</xsl:stylesheet> 
+0

ありがとうございます。それは簡単です、私は関数の合計がxslt 1.0で動作しないと思ったので、私は非常に複雑な解決策を作る。もう一度あなたの助けをありがとう:) – jeffers

2

私は必要な結果をパラメータ FormOfPaymentによってGrossValue額の合計です。

、なぜあなたは、単に実行しない:場合

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:param name="FormOfPayment"/> 

<xsl:template match="/Documents"> 
    <total> 
     <xsl:value-of select="sum(Document[FormOfPayment=$FormOfPayment]/GrossValue)"/> 
    </total> 
</xsl:template> 

</xsl:stylesheet> 

を追加しました - それはあなたの試みから思わとして - XMLソースの金額には10進数のカンマが使用されます小数のドット(図示XMLは、この問題ない展示を行いますが)、そしてあなたがXSLT 1.0に限定されている、私はあなたが行うことをお勧め:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exsl="http://exslt.org/common" 
extension-element-prefixes="exsl"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:param name="FormOfPayment"/> 

<xsl:template match="/Documents"> 
    <xsl:variable name="amounts"> 
     <xsl:for-each select="Document[FormOfPayment=$FormOfPayment]"> 
      <amount> 
       <xsl:value-of select="translate(GrossValue, ',', '.')"/> 
      </amount> 
     </xsl:for-each> 
    </xsl:variable> 
    <total> 
     <xsl:value-of select="sum(exsl:node-set($amounts)/amount)"/> 
    </total> 
</xsl:template> 

</xsl:stylesheet> 
+0

あなたのソリューションをありがとう。それも動作します。ありがとう。 – jeffers

関連する問題