Android

Test for non-existant attribute in XPath

aucd29 2013. 10. 8. 14:53
http://www.eggheadcafe.com/software/aspnet/33499401/howto-test-for-nonexistant-attribute-in-xpath.aspx

Hi,

sorry if my question may sound academic, but what is the common
practice, when selecting nodes that do not have a specific attribute:

(1) //node[concat(@attribute, '') = ''

or

(2) //node[not(boolean(@attribute))]

Thanks,
Stefan




Martin Honnen mentioned in XPath 1.0 I would do
//node[not(@attribute)]
but your (2) is also possible and does the same, only more verbose.

In XPath 2.0 you could also do
//node[not(exists(@attribute))]
which is also more verbose but might be clearer to readers not familiar
with XPath intrinsics like how not() applied to a node-set (respectively
sequence) work.


StefanM responded with martin,

I think I will use "not(@attribute)", as this more clarifies the fact,
that the attribute does not exist. It's a pity that exists() is only
available with XPath 2.0...

Thanks for your immediate feedback,
Stefan