In the above xml sample I would like to select all the books that belong to class foo and not in class bar by using xpath.
By padding the @class
value with leading and trailing spaces, you can test for the presence of " foo " and " bar " and not worry about whether it was first, middle, or last, and any false positive hits on "food" or "barren" @class
values:
/bookstore/book[contains(concat(' ',@class,' '),' foo ')
and not(contains(concat(' ',@class,' '),' bar '))]
Although I like Mads solution: Here is another approach for XPath 2.0:
/bookstore/book[
tokenize(@class," ")="foo"
and not(tokenize(@class," ")="bar")
]
Please note that the following expressions are both true:
("foo","bar")="foo" -> true
("foo","bar")="bar" -> true
XPath 2.0:
/*/*[for $s in concat(' ',@class,' ')
return
matches($s, ' foo ')
and
not(matches($s, ' bar '))
]
Here no tokenization is done and $s is calculated only once.
Or even:
/*/book[@class
[every $t in tokenize(.,' ') satisfies $t ne 'bar']
[some $t in tokenize(.,' ') satisfies $t eq 'foo']
]