-1

特定のノード値に基づいて xml をグループ化しようとしています。

<bus:TaxList>
<bus:VoPaidTax>
<bus:TaxAmount>4.45</bus:TaxAmount>
<bus:TaxCode>10</bus:TaxCode>
<bus:TaxRate>0.05</bus:TaxRate>
<bus:TaxType>VAT</bus:TaxType>
</bus:VoPaidTax>
<bus:VoPaidTax>
<bus:TaxAmount>6.23</bus:TaxAmount>
<bus:TaxCode>12</bus:TaxCode>
<bus:TaxRate>0.07</bus:TaxRate>
<bus:TaxType>VAT</bus:TaxType>
</bus:VoPaidTax>
<bus:VoPaidTax>
<bus:TaxAmount>6.45</bus:TaxAmount>
<bus:TaxCode>10</bus:TaxCode>
<bus:TaxRate>0.05</bus:TaxRate>
<bus:TaxType>VAT</bus:TaxType>
</bus:VoPaidTax>
<bus:VoPaidTax>
<bus:TaxAmount>9.03</bus:TaxAmount>
<bus:TaxCode>12</bus:TaxCode>
<bus:TaxRate>0.07</bus:TaxRate>
<bus:TaxType>VAT</bus:TaxType>
</bus:VoPaidTax>
</bus:TaxList

次に、同じ TaxCode と Rate を持つ VoPaidTax グループを追加します。

<bus:TaxList>
<bus:VoPaidTax>
<bus:TaxAmount>10.90</bus:TaxAmount>
<bus:TaxCode>10</bus:TaxCode>
<bus:TaxRate>0.05</bus:TaxRate>
<bus:TaxType>VAT</bus:TaxType>
</bus:VoPaidTax>
<bus:VoPaidTax>
<bus:TaxAmount>15.23</bus:TaxAmount>
<bus:TaxCode>12</bus:TaxCode>
<bus:TaxRate>0.07</bus:TaxRate>
<bus:TaxType>VAT</bus:TaxType>
</bus:VoPaidTax>
</bus:TaxList

そして、ユニークなものを残してください。

私はこのようなことを試しましたが、うまくいかないようですここで何が間違っていますか:

<xsl:template    match="bus:TaxList">
<xsl:for-each select="bus:VoPaidTax[not(bus:TaxCode = ../preceding-  sibling::*/bus:VoPaidTax/bus:TaxCode) and not(bus:TaxRate= ../preceding-  sibling::*/bus:VoPaidTax/bus:TaxRate)]">
<xsl:call-template name="taxCorrection">
<xsl:with-param name="TaxCode">
<xsl:value-of select="bus:TaxCode"/>
</xsl:with-param>
<xsl:with-param name="percent">
<xsl:value-of select="bus:TaxRate"/>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="taxCorrection">
<xsl:param name="TaxCode"/>
<xsl:param name="percent"/>
<xsl:variable name="sumTaxRate">
<xsl:value-of select="sum(../bus:VoPaidTax[bus:TaxCode = $TaxCode and bus:TaxRate =$percent]/bus:TaxAmount)" />
</xsl:variable>

</xsl:template>

問題は、このテンプレートが複数回呼び出され、合計と前の兄弟が機能していないように見えることです。ここで何が間違っていますか?? XSLT 1.0 を使用しています。誰か助けてください。

4

2 に答える 2

1

実際、グループ合計の数値を揃えるためにキーが必要なため、Muenchian Grouping を特に検討してください。

<xsl:key name="taxgrp" match="bus:VoPaidTax" use="concat(bus:TaxCode, bus:TaxRate)" />

  <xsl:template match="bus:TaxList">
   <bus:TaxList>            
    <xsl:for-each select="bus:VoPaidTax[generate-id()
                 = generate-id(key('taxgrp', concat(bus:TaxCode, bus:TaxRate))[1])]">
       <bus:VoPaidTax>
         <bus:TaxAmount>
           <xsl:value-of select="sum(key('taxgrp', 
                                     concat(bus:TaxCode, bus:TaxRate))/bus:TaxAmount)"/>
         </bus:TaxAmount>
         <xsl:copy-of select="*[not(local-name()='bus:TaxAmount')]"/>
      </bus:VoPaidTax>
    </xsl:for-each>      
   </bus:TaxList>
  </xsl:template>    

出力

<bus:TaxList>
  <bus:VoPaidTax>
    <bus:TaxAmount>10.9</bus:TaxAmount>
    <bus:TaxCode>10</bus:TaxCode>
    <bus:TaxRate>0.05</bus:TaxRate>
    <bus:TaxType>VAT</bus:TaxType>
  </bus:VoPaidTax>
  <bus:VoPaidTax>
    <bus:TaxAmount>15.26</bus:TaxAmount>
    <bus:TaxCode>12</bus:TaxCode>
    <bus:TaxRate>0.07</bus:TaxRate>
    <bus:TaxType>VAT</bus:TaxType>
  </bus:VoPaidTax>
</bus:TaxList>
于 2015-10-25T01:57:58.473 に答える
0
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>
<xsl:key name="taxgrp" match="bus:VoPaidTax" use="concat(generate-id(..),bus:TaxCode, bus:TaxRate)" />

<xsl:template match="bus:BsAddOrderPaymentRequestPayload/bus:Request/bus:TotalPaidAmount/bus:TaxList">

<bus:TaxList>            
<xsl:for-each select="bus:VoPaidTax[generate-id()
             = generate-id(key('taxgrp', concat(generate-id(..),bus:TaxCode, bus:TaxRate))[1])]">
   <bus:VoPaidTax>
     <bus:TaxAmount>
       <xsl:value-of select="sum(key('taxgrp', 
                                 concat(generate-id(..),bus:TaxCode, bus:TaxRate))/bus:TaxAmount)"/>
     </bus:TaxAmount>
     <xsl:copy-of select="*[not(self::bus:TaxAmount)]"/>
  </bus:VoPaidTax>
</xsl:for-each>      
</bus:TaxList>
</xsl:template> 
</xsl:stylesheet>

グループ化をドキュメント全体ではなく特定のノードに保持するために、各キーに generate-id(..) を追加しました。

于 2015-10-25T13:27:59.957 に答える