dacracot
dacracot

Reputation: 22358

Need an XPath that will check an attribute value of any child, grandchild, or great grandchild, etc

So given this XML...

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <tree dah="false">
        <tree dah="false">
            <tree dah="false"/>
            <tree dah="false"/>
        </tree>
        <tree dah="false">
            <tree dah="true"/>
            <tree dah="false"/>
        </tree>
    </tree>
</root>

...I need an XPath that will evaluate to true since there is at least one tree/@dah='true'.

But that would evaluate to false if the XML looked like this...

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <tree dah="false">
        <tree dah="false">
            <tree dah="false"/>
            <tree dah="false"/>
        </tree>
        <tree dah="false">
            <tree dah="false"/>
            <tree dah="false"/>
        </tree>
    </tree>
</root>

Also, the tree nodes may be any depth. I have three levels in my example, but it could go much deeper.

Upvotes: 2

Views: 686

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

Use:

boolean(/root//tree[@dah='true'])

or

boolean((/root//tree[@dah='true'])[1])

Both expressions are equivalent, but the second would be more efficient with dumb (non-optimizing) XPath engines.

The result is true() if there exists a tree element in the XML document with a dah attribute with value 'true' -- otherwise the result is false().

Upvotes: 2

MooGoo
MooGoo

Reputation: 48240

/root//tree[@dah='true']

Upvotes: 1

Related Questions