55

I have simple xpath

 /products/product[contains(categorie,'Kinderwagens')] 

It works but now how do I filter on multiple categorie values

like

/products/product[contains(categorie,'Kinderwagens')]  +  
/products/product[contains(categorie,'Kinderwagens')] 
vhs
  • 9,316
  • 3
  • 66
  • 70
Bram
  • 615
  • 2
  • 6
  • 8

4 Answers4

91

Will this work?

/products/product[contains(categorie,'Kinderwagens') or contains(categorie,'Wonderwagens')]

There is a similar question here

Community
  • 1
  • 1
Curious
  • 2,783
  • 3
  • 29
  • 45
19

Do you really want contains()? Very often, people use contains() to test whether a node contains a value when they should be using "=". The difference is that if the categorie element has the string value 'Kinderwagens', then categorie = 'wagens' is false but contains(categorie, 'wagens') is true.

If you actually intended '=', then in XPath 2.0 you can write [categorie = ('Kinderwagens', 'Wonderwagens')]. If you're still using XPath 1.0, you need two separate comparisons with an 'or'.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Submitted [new question](https://stackoverflow.com/q/46503195/712334) for use with attribute values. – vhs Sep 30 '17 at 13:36
7

There are many ways:

  1. //*[contains(text(), 'Kinderwagens') or contains(text(), 'Kinderwagens')]
  2. //product[contains(text(), 'Kinderwagens') or contains(text(), 'Kinderwagens')]
  3. //products/product[contains(text(), 'Kinderwagens') or contains(text(), 'Kinderwagens')]
  4. //products/product[contains(categorie, 'Kinderwagens') or contains(categorie, 'Kinderwagens')]

Note: | (Bitwise OR) operator don't be use

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
-3

You can pass the multiple text as like below

//a[contains(text(),'JB-' | 'ABC')]
vahdet
  • 6,357
  • 9
  • 51
  • 106
siva g
  • 1
  • 1
  • 4
    This didn't work for me. The xpath needs to be edited slightly like this: `//a[contains(text(),'JB-') or contains(text(), 'ABC')]` – SandstormNick Aug 13 '19 at 11:50