Tadeáš Jílek
Tadeáš Jílek

Reputation: 2833

Xpath - selecting nearest element after plain text

Let's say we have the following structure :

some text
<img src="#" />
sdfsdf
sdfsdf

plaintext-identificator-123

<img src="1" /> //get
<img src="2" /> //get
<img src="3" /> //not get

I need to get the nearest 2 elements after "plaintext-identificator-123".

In this case, the desired result would be to get 2 elements with srcs 1 and 2.

Upvotes: 1

Views: 95

Answers (2)

Jack Fleeting
Jack Fleeting

Reputation: 24928

Try this on your actual structure and see if it works (it works for your sample in the question):

(//img['plaintext-identificator-123']/following-sibling::img)[position()<3]

if you want the attribute values of the src attribute of the target img nodes, change it to:

(//img['plaintext-identificator-123']/following-sibling::img)[position()<3]/@src

Edit:

Following the comments below, try this:

(//text()[contains(.,'plaintext-identificator-123')]/preceding-sibling::img/following-sibling::img)[position()<3]/@src

Use http://xpather.com/DOJz7nl1 (try it with and without the indentificator).

Upvotes: 1

Alexey R.
Alexey R.

Reputation: 8686

This is one of the ways:

//text()[contains(., 'plaintext-identificator-123')]/following-sibling::img[position()<3]

Upvotes: 1

Related Questions