Harrison
Harrison

Reputation: 2690

Select different nodes with different depths in one xpath

<div>
  <p>a</p>
  <ul>
    <li></li>
  </ul>
  <other-tags></other-tags>
<div>

I would like to select p and li tags in the above example.

I can do it using //div/p|//div/ul/li, but is it possible to achieve the same without |, in other words, not repeating //div. I tried //div/*[self::p or self::ul/li], but it selected p and ul tags.

Upvotes: 1

Views: 125

Answers (2)

Jack Bashford
Jack Bashford

Reputation: 44125

All you should do is use //p|//li. Also, it helps to research stuff before posting it on StackOverflow, as I found this information here. Why are you trying to avoid using | in your XPath?

Upvotes: 0

kjhughes
kjhughes

Reputation: 111696

This XPath,

//*[self::p or self::li]

will select all p and li elements in the document.

This XPath,

//div[@id="i1"]//*[self::p or self::li]

will select all p and li elements under the targeted div element(s).

Upvotes: 1

Related Questions