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
This XPath,
will select all
p
elements with a single child node that is aem
element.Here is another Xpath, which will select the paragraph with only
em
and no direct text.//p[not(text())][em]