Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine these xpath expressions?

Tags:

xpath

I have two Xpath expressions ...

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

How do I write a single xpath expression that combines the two above and would return the same node-set as combining the results of running each of the expressions above individually?

like image 422
Dave Avatar asked May 01 '12 18:05

Dave


1 Answers

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.

like image 75
Dimitre Novatchev Avatar answered Nov 09 '22 00:11

Dimitre Novatchev