XPath OR operator for different nodes

Xpath

Xpath Problem Overview


How can I do with XPath:

//bookstore/book/title or //bookstore/city/zipcode/title

Just //title won't work because I also have //bookstore/magazine/title

p.s. I saw a lot of or examples but mainly with attributes or single node structure.

Xpath Solutions


Solution 1 - Xpath

All title nodes with zipcode or book node as parent:

Version 1:

//title[parent::zipcode|parent::book]

Version 2:

//bookstore/book/title|//bookstore/city/zipcode/title

Version 3: (results are sorted based on source data rather than the order of book then zipcode)

//title[../../../*[book or magazine] or ../../../../*[city/zipcode]]

or - used within true/false - a Boolean operator in xpath

| - a Union operator in xpath that appends the query to the right of the operator to the result set from the left query.

Solution 2 - Xpath

If you want to select only one of two nodes with union operator, you can use this solution: (//bookstore/book/title | //bookstore/city/zipcode/title)[1]

Solution 3 - Xpath

It the element has two xpath. Then you can write two xpaths like below:

xpath1 | xpath2

Eg:

//input[@name="username"] | //input[@id="wm_login-username"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser569008View Question on Stackoverflow
Solution 1 - XpathStephanView Answer on Stackoverflow
Solution 2 - XpathTimView Answer on Stackoverflow
Solution 3 - XpathSubrahmanya PrasadView Answer on Stackoverflow