Reputation: 22358
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
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