How do I combine these xpath expressions?

21,323

Solution 1

How about:

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

which is more or less self-explanatory.

Solution 2

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.

Share:
21,323

Related videos on Youtube

Dave
Author by

Dave

Updated on November 08, 2020

Comments

  • Dave
    Dave over 3 years

    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?