How do I combine these xpath expressions?

In general this Xpath expression:

expr1 | expr2

selects the union of all nodes selected by expr1 and all nodes selected by expr2.

The | character denotes the XPath union operator.

You can use the union operator in any case when you want the union of the nodes selected by several XPath expressions to be returned.

In this concrete case:

 //*[@id='gutter']/p[strong[text()='Date:']]/text()
|
 //*[@id='gutter']/p[strong[text()='Time:']]/text()

While this expression can be optimized, it has the advantage that the union operator "works" in all such cases, can be expressed almost mechanically, saves time and eliminates the possibility for introducing error by a more complicated refactoring.


How about:

//*[@id='gutter']/p[strong[text()='Date:' or text()='Time:']]/text()

which is more or less self-explanatory.

Tags:

Xpath