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>

4

1 回答 1

0

试试这个换行

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

通过这个

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

您仍然必须找到如何列出发票编号,但在此之后它会很容易。

于 2013-08-20T18:52:03.200 回答