XML Xpath - select where element = multiple values

Quite a simple question, but rather difficult to find what I want via a search, here or Google.

Most people ask how you can select a node / element with multiple conditions.

how URL/books[title="Harry Potter" and author="JKRowling"]

I want to know if there is a way to shorten this syntax if you have multiple possibilities for a single attribute.

In other words

URL/books[price=1 or price=2 or price=3 or price=8 or price=15]

      

Is there a way to shorten this syntax?

Like URL/books[price=1,2,3]

or [price in (1,2,3)]

----, this is obviously wrong, but will make things easier.

+3


source to share


1 answer


XPath 2.0 will allow

URL/books[price=(1,2,3)]

      

Operator =

(and !=

, <

, >

, <=

and >=

) in XPath has an implicit existential quantifier if one or both of its operands are sequences - X = Y

true if any member of the sequence X

is the sequence of any member Y

. Note that this means that it X != Y

does not necessarily coincide with not(X = Y)

- the former means that there is some pair of X and Y values ​​that are not equal (and there may also be other pairs that are equal), the latter means that there are no equal pairs at all.

However XPath 1.0 does not support sequences of atomic values ​​like this, its only multivalued data type is the node set. If you somehow introduced a variable with a list of values ​​as XML, eg.



<prices>
  <price>1</price>
  <price>2</price>
  <price>3</price>
</prices>

      

then you could say

URL/books[price = $var/prices/price]

      

but (a) depends on how and how your XPath engine supports variable bindings and (b) is probably less clear than just enumeration price = 1 or price = 2 ...

anyway.

+3


source







All Articles