Search code examples
xmlxpathxsltxslt-1.0xpath-1.0

Multiple filters while doing Sum in XSLT 1


XML :

<A>
    <B>
        <C name="test name1" age="10">
            <id>1</id>
            <address/>
        </C>
        <C name="test name2" age="20">
            <id>2</id>
            <address>test address</address>
        </C>
        <C name="test name3" age="30">
            <id>3</id>
            <address/>
        </C>
    </B>
</A>

In this XML, I want to get the sum of all C's age where name doesn't contain a String & address is empty.

XSLT/XPath snippets that I've tried :

<xsl:variable name="total-age" select="sum(A/B/C[not(contains(@name,'testString')) and (address = '')]/@age)" />

and

<xsl:variable name="total-age" select="sum(A/B/C[not(contains(@name,'testString'))] [address = '']/@age)" />

If I use just one filter say <xsl:variable name="total-age" select="sum(A/B/C [address = '']/@age)" /> or <xsl:variable name="total-age" select="sum(A/B/C[not(contains(@name,'testString'))]/@age)" /> It'll work for that particular filter however when I add both the filters it's not working. How to make both the filters work ? I'm using XSLT/XPATH 1 version


Solution

  • I want to get the sum of all C's age where name doesn't contain a String & address is empty.

    I believe that would be:

    sum(/A/B/C[not(contains(@name,'testString')) and not(address/text())]/@age)