0

使用 XPath 和 HTML Agility Pack,我需要destination使用color:#ff00ff.

我的 HTML 如下所示:

<table>
   <tr style="color:#ff00ff">
      <td></td>
   </tr>
   <tr>
      <td>destination</td>
   </tr>
   <tr>
      <td></td>
   </tr>
   <tr>
      <td>not destination</td>
   </tr>
</table>
4

2 回答 2

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

选择<tr>具有style="color:#ff00ff"并从那里开始的第<td>一个以下第一个子项的文本<tr>

为了额外的安全,您可以使用:

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

这从属性值中删除了所有空格,因此事情变得更加独立于 HTML 源代码。

于 2009-11-05T09:42:24.160 回答
0

使用 jQuery 它可能看起来像这样:

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

不过,这在很大程度上取决于您的确切文档结构和样式定义。它将找到任何具有包含字符串“color:#ff00ff”(确切地说,没有空格等)的样式的 tr。然后它将从该行中选择下一个兄弟行并从它的所有直接子代中获取文本内容。在您的情况下,这将是单列元素。

于 2009-11-04T18:00:58.603 回答