2013-08-20 69 views
0

也許有人可以幫我解決這個問題。我一直在網上搜索一段時間,但找不到解決方案在XSLT 1.0中添加值併合並字符串

我想按供應商合併發票,添加金額並連接發票編號。

如果InvoiceNo的值是「###」,我只想添加金額。

我可以創建一個xslt,添加金額,但還不知道 發票號碼的連接如何工作。

XML輸入:

<Payments> 
<Invoices> 
    <Invoice> 
     <InvoiceNo>###</InvoiceNo> 
     <Amount>100</Amount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNo>N1</InvoiceNo> 
     <Amount>100</Amount> 
     <Supplier> 
      <Name>Supp1</Name> 
     </Supplier> 
    </Invoice> 
</Invoices> 
<Invoices> 
    <Invoice> 
     <InvoiceNo>###</InvoiceNo> 
     <Amount>200</Amount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNo>N2</InvoiceNo> 
     <Amount>200</Amount> 
     <Supplier> 
      <Name>Supp1</Name> 
     </Supplier> 
    </Invoice> 
</Invoices> 
<Invoices> 
    <Invoice> 
     <InvoiceNo>###</InvoiceNo> 
     <Amount>1</Amount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNo>M1</InvoiceNo> 
     <Amount>1</Amount> 
     <Supplier> 
      <Name>Supp2</Name> 
     </Supplier> 
    </Invoice> 
</Invoices> 
<Invoices> 
    <Invoice> 
     <InvoiceNo>###</InvoiceNo> 
     <Amount>2</Amount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNo>M2</InvoiceNo> 
     <Amount>2</Amount> 
     <Supplier> 
      <Name>Supp2</Name> 
     </Supplier> 
    </Invoice> 
</Invoices></Payments> 

預期結果:

<Payments> 
<Invoices> 
    <Invoice> 
     <InvoiceNo>###</InvoiceNo> 
     <Amount>300</Amount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNo>N1,N2</InvoiceNo> 
     <Amount>300</Amount> 
     <Supplier> 
      <Name>Supp1</Name> 
     </Supplier> 
    </Invoice> 
</Invoices> 
<Invoices> 
    <Invoice> 
     <InvoiceNo>###</InvoiceNo> 
     <Amount>3</Amount> 
    </Invoice> 
    <Invoice> 
     <InvoiceNo>M1,M2</InvoiceNo> 
     <Amount>3</Amount> 
     <Supplier> 
      <Name>Supp2</Name> 
     </Supplier> 
    </Invoice> 
</Invoices></Payments> 

到目前爲止:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
<xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="no"></xsl:output> 
<xsl:key name="namekey" match="Invoices" use="Invoice/Supplier/Name"/> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="Payments"> 
    <xsl:copy>   
     <xsl:for-each select="Invoices[generate-id()=generate-id(key('namekey',Invoice/Supplier/Name)[1])]"> 
      <xsl:variable name="AMOUNT" select="0.5*sum(key('namekey',Invoice/Supplier/Name)/Invoice/Amount)"> </xsl:variable> 
      <xsl:copy> 
       <xsl:for-each select="Invoice"> 
        <xsl:copy> 
         <Amount><xsl:value-of select="$AMOUNT"/></Amount>           
         <InvoiceNo><xsl:value-of select="InvoiceNo"/></InvoiceNo> 
         <xsl:copy-of select="Supplier"/> 
        </xsl:copy> 
       </xsl:for-each> 
      </xsl:copy> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

+0

請顯示您的努力以獲得更好的答案。 – Shivaay

+0

將xslt添加到原始帖子中。 – user2699474

回答

0

試試這個更改的行

<xsl:key name="namekey" match="Invoices" use="Invoice/Supplier/Name"/> 

通過這一個

<xsl:key name="namekey" match="/Payments/Invoices" use="Invoice/Supplier/Name"/> 

你仍然需要找到如何列出的發票號碼,但畢竟這將是一個容易的。

+0

非常感謝你! – user2699474