【问题标题】:Adding values and combining strings in XSLT 1.0在 XSLT 1.0 中添加值和组合字符串
【发布时间】:2013-08-20 10:30:26
【问题描述】:

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

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

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

【问题讨论】:

  • 请展示你的努力以获得更好的答案..
  • 将 xslt 添加到原帖中。

标签: string xslt node-set


【解决方案1】:

试试这个换行

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

这个

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

您仍然需要找到如何列出发票编号的方法,但之后就很容易了。

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 2021-10-10
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-02
  • 2011-11-23
  • 1970-01-01
相关资源
最近更新 更多