Adding values and combining strings in XSLT 1.0

231 views Asked by At

maybe someone can help me with this problem. I have been searching the net for a while but I couldn't find the solution

I want to combine the invoices by supplier, adding the amounts and concatenating the invoice numbers.

If the value of InvoiceNo is "###", I only want to add the amounts.

I could create an xslt that adds the amounts, but couldn't figure out yet how the concatenation of the invoice numbers works.

XML input:

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

Expected Result:

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

So far:

<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

There are 1 answers

0
jvverde On

Try this change the line

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

by this one

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

You still have to find how to list the invoices numbers, but after this it will be a easy.