简体   繁体   中英

XSLT 1.0 Muenchian Grouping

I've been working on some examples using the Muenchian method of group, and now have one working where by key and value for summing are siblings within the same parent node but now I'm looking at an XML input where the structure is slightly different.

This is my input

<?xml version="1.0" encoding="UTF-8"?>
<ArrayList>
    <Item>
        <amounts>
            <amount>1000.00</amount>
        </amounts>
        <invoice>
            <customerId>1234</customerId>
        </invoice>
    </Item>
    <Item>
        <amounts>
            <amount>1500.00</amount>
        </amounts>
        <invoice>
            <customerId>7755</customerId>
        </invoice>
    </Item>
    <Item>
        <amounts>
            <amount>800.00</amount>
        </amounts>
        <invoice>
            <customerId>1018</customerId>
        </invoice>
    </Item>
</ArrayList>

And I want to get to

<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer>
        <customerID>1234</customerID>
        <totalAmount>1800</totalAmount>
    </customer>
    <customer>
        <customerID>7755</customerID>
        <totalAmount>1500</totalAmount>
    </customer>
</customers>

Initially I was getting my distinct customerId's OK but total amount was 0.00 and now playing around with it so long my XSL is no longer valid. I was trying to avoid posting this until I'd managed to resolve it but I've become blind to it now and can't spot the problem. Even once I have sorted that I know it won't give me what I want.

This is my XSL as it stands:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes" />

<xsl:key name="group" match="Item" use="invoice/customerId" />

    <xsl:template match="ArrayList">
        <customers>
            <xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId[1])]"/>
        </customers>
    </xsl:template>
    
    <xsl:template match="Item">
        <customer>
            <xsl:copy-of select="customerID"/>
            <totalAmount><xsl:value-of select="sum(key('group', invoice/customerId)/amounts/amount)" /></totalAmount>
        </customer>
    </xsl:template>

</xsl:stylesheet>

Thanks

There is a missing closing ) in the apply-templates expression, make that <xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId[1]))]"/> .

On the other hand, Muenchian grouping as I suggested it on Twitter would be <xsl:apply-templates select="Item[generate-id() = generate-id(key('group', invoice/customerId)[1])]"/> .

Finally, in the context of an Item element you want eg <xsl:copy-of select="invoice/customerId"/> instead of <xsl:copy-of select="customerID"/> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM