开发者

how to select xml node using near element

Using XPath and the HTML Agility Pack, I need to select the destination text using color:#ff00ff.

My HTML looks like this:

<table>
   <tr style="color:#ff00ff">
      <td></td>
   </tr>
  开发者_开发百科 <tr>
      <td>destination</td>
   </tr>
   <tr>
      <td></td>
   </tr>
   <tr>
      <td>not destination</td>
   </tr>
</table>


/table/tr[@style = "color:#ff00ff"]/following-sibling::tr[1]/td[1]/text()

Selects the <tr> that has style="color:#ff00ff", and going from there, the text of the first <td> child of the first following <tr>.

For extra safety, you could use:

tr[translate(@style, ' ', '') = "color:#ff00ff"]

This removes any spaces from the attribute value, so things get a bit more independent from the HTML source.


Using jQuery it might look something like this:

$('tr[style*="color:#ff00ff"]').next('tr').children().text();

This is heavily dependent on your exact document structure and style definition, though. It will find any tr that has a style containing the string "color:#ff00ff" (exactly, no spaces, etc). From that row it will then select the next sibling row and get the text contents from all of it's direct children. In your case, this would be the single column element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜