skip to Main Content

Let’s say I have this example:

<div>
<p>some text <em>emphasized text</em> some other text</p>
<p><em>The paragraph I want to capture</em></p>
<p>some text <em>emphasized text</em> some other text and <em>other em text</em> until the end.</p>
</div>

What I want to select is the second paragraph (but it may be third or first as well). The thing is that here p and em are adjacent. There is no text between <p> and <em>, not at the beginning nor in the end. All text is inside <em>xyz</em>.

How can I get it with XPath query ?

I tried //p/em, //p/child:em, //em/parent:p, all these select the three paragraphs as all em are children of p.
//p[starts-with(.,'./em')] didn’t help either.

2

Answers


  1. This XPath,

    //p[count(node())=1][em]
    

    will select all p elements with a single child node that is a em element.

    Login or Signup to reply.
  2. Here is another Xpath, which will select the paragraph with only em and no direct text.

    //p[not(text())][em]

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search