-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

现在我想添加具有相同税码和税率的 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 分组,尤其是因为需要键来对齐数字以进行分组求和。

<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 回答