xpath to select nodes excluding by attribute value list
-
21-06-2021 - |
Вопрос
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'))]
Решение
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.
Другие советы
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, '|')
)
)]
Maybe something like this??
/item[not(contains('id1 id2', @uid))]
At least in Xpath 2 general comparisons on sequences are performed pairwise and existentially, so you can write:
/item[not(@uid = ('id1','id2'))]
Не связан с StackOverflow