xpath to select nodes excluding by attribute value list
22,615
Solution 1
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.
Solution 2
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, '|')
)
)]
Solution 3
Maybe something like this??
/item[not(contains('id1 id2', @uid))]
Related videos on Youtube
Author by
Aitorito
Updated on July 17, 2020Comments
-
Aitorito almost 4 years
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'))]
-
Arne almost 12 yearsworks, but risky for changes, as
contains(x, y)
is true for non-existing y. -
Aitorito almost 12 years@Arne This one is the shortest but in what cases would it fail? The attribute we use to filter is a unique id without spaces
-
Aitorito almost 12 yearsYou were right, the other method if i had two items like 'id1' and 'id11' it would ignore both by error
-
Nickmaovich about 10 yearsI needed to build very long XPath in .NET. Instead of using stuff like LINQ Aggregate method with seed, you can just simply join strings with '|', add trailing and leading, and use it. Thanks!