How to merge multiple nodes XPath

Viewed 42

I need to solve this problem. I have an XML like this:

<root>
    <CustomLines>
        <Custom>
            <VAR1>00001</VAR1>
            <VAR2>5</VAR2>
        </Custom>
        <Custom>
            <VAR1>00001</VAR1>
            <VAR2>2</VAR2>
        </Custom>
        <Custom>
            <VAR1>00003</VAR1>
            <VAR2>9</VAR2>
        </Custom>
        <Totals>
            <Tot>
                <VAR3>00001</VAR3>
            </Tot>
            <Tot>
                <VAR3>00003</VAR3>
            </Tot>
        </Totals>
    </CustomLines>
</root>

I need to do 2 groups (00001 and 00003) and then sum for each group VAR2.

The result will be like this:

    00001 7
    00003 9

I tried but I'm not able to group the 2 Custom Nodes.

1 Answers

I don't think you can do this with just XPath 1.0, but you can do it with XPath 2.0:

for $var3 in //VAR3 return concat($var3,' ',string(sum(//Custom[VAR1=$var3]/VAR2)))
Related