Aitorito
Aitorito

Reputation: 255

xpath to select nodes excluding by attribute value list

I would like to know if there is shorter approach to selecting a list of nodes ignoring the ones that have an attribute value specified in a list

Working example:

/item[not(@uid='id1') and not(@uid='id2')]

Desired alternative:

/item[not(@uid in('id1','id2'))]

Upvotes: 24

Views: 25223

Answers (4)

BeniBela
BeniBela

Reputation: 16917

At least in Xpath 2 general comparisons on sequences are performed pairwise and existentially, so you can write:

/item[not(@uid = ('id1','id2'))]

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

If the list of attribute names is very long, the following is a good way to handle this situation:

//item[not(contains('|attr1|attr2|attr3|attr4|attr5|attr6|attrN|', 
                    concat('|', @uid, '|')
                    )
           )]

Upvotes: 8

Arne
Arne

Reputation: 2146

You can either use regex - if your xpath implementation supports it - or write

/item[not(@uid='id1' or @uid='id2')]

which might be a bit shorter.

Upvotes: 28

VSP
VSP

Reputation: 2393

Maybe something like this??

/item[not(contains('id1 id2', @uid))]

Upvotes: 4

Related Questions