在XSLT 1.0中添加值和组合字符串

时间:2013-08-20 10:30:27

标签: string xslt node-set

也许有人可以帮我解决这个问题。我一直在网上搜索一段时间,但我找不到解决方案

我希望按供应商合并发票,添加金额并连接发票编号。

如果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>

1 个答案:

答案 0 :(得分:0)

尝试更改行

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

这一个

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

您仍然需要找到如何列出发票号码,但在此之后它将很容易。