Can I choose a node whose next (or previous) sibling meets certain criteria using XPath?

html html-agility-pack xpath

Question

I am reformatting an HTML document using the Agility Pack, and I've run into a limitation of my understanding of XPath.

In the document I'm working with, the following is a common construct:

1282

Which is built like this:

128<img src="" style="display: none;" alt="^(" /><sup>2</sup><img src="" style="display: none;" alt=")" />

So, when you select that and copy it to the clipboard it turns into:

128^(2)

Now, I would like to use XPath to remove these img tags.

Here is what I have so far:

//img[@alt='^('  ???/sup]

How do I select an element based on existence of an immediate sibling?

1
2
7/9/2010 3:37:26 PM

Accepted Answer

apparently it is something like this:

//img[@alt='^(' and following-sibling::*[1][self::sup]]

That is (and, I'm guessing, here):

//img

An img (anywhere)...

[@alt='^(' and  ...  ]

... whose alt attribute is '^(' and ...

following-sibling::*[1]

... whose first following sibling ...

[self::sup]

... can call itself a sup.

2
5/21/2013 6:17:18 PM

Popular Answer

Off the top of my head, make it a compound condition. Something like:

//img[@alt='^('  ???/sup AND preceding-sibling::*[1] AND following-sibling::*[1]]


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow