Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding an adjacent node in xml through xpath

Tags:

xml

xpath

i have a deeply nested structure (actually parsing out xhtml, so lots of nasty), like so:

<tr>
  <td>
    <font id="blah">
      stuff
    </font>
  </td>
</tr>
<tr>
  <td>
      more stuff
  </td>
</tr>

and this repeats in a long table. i need an xpath expression that will select the second font tag (or rather it's text()). i was looking at the preceding-sibling axis, but something isn't quite working right.

something along the lines of (and pardon me if this is ridiculous, my xpath is rusty)

//tr[preceding-sibling::tr/td/font]/td/text()
like image 309
kolosy Avatar asked Mar 22 '26 05:03

kolosy


1 Answers

Use:

(//tr/td[font])[2]/font/text()

This means:

Select all text-node children of all font elements that are children of the second td in the document that has a font child and is itself a child of some tr element.

As you can see, no preceeding axis is necessary.

like image 108
Dimitre Novatchev Avatar answered Mar 23 '26 19:03

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!