XPath contains one of multiple values

83,325

Solution 1

Will this work?

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

There is a similar question here

Solution 2

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'.

Solution 3

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

Share:
83,325

Related videos on Youtube

Bram
Author by

Bram

Updated on July 15, 2021

Comments

  • Bram
    Bram almost 3 years

    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')] 
    
  • imz -- Ivan Zakharyaschev
    imz -- Ivan Zakharyaschev almost 12 years
    The question is about OR, isn't it?
  • vhs
    vhs almost 7 years
    Note to self. Update answer with 5 more years of XPath advancement.
  • vhs
    vhs almost 7 years
    Submitted new question for use with attribute values.
  • SandstormNick
    SandstormNick almost 5 years
    This didn't work for me. The xpath needs to be edited slightly like this: //a[contains(text(),'JB-') or contains(text(), 'ABC')]