【问题标题】:XPath expression that matches the bgcolor attribute of a TR element匹配 TR 元素的 bgcolor 属性的 XPath 表达式
【发布时间】:2012-05-13 09:37:29
【问题描述】:

对没有nameid 生成的表行使用HtmlAgilityPack。相反,我需要根据行的 bgcolor 属性中包含的值进行选择:

我了解 XPath 将返回 name 属性 = display 的所有行:

foreach(HtmlNode cell in doc.DocumentElement.SelectNodes("//tr[@name='display']/td")

给定下面的代码sn-p,当行的bgcolor ="#FFFFFF"> 时,什么表达式会选择所有元素?

我试过了:SelectNodes(//tr[@bgcolor='#FFFFFF']/td")

> <tr bgcolor="#EAF2FA">
>     <td colspan="2">
>         <font style="font-family: sans-serif; font-size:12px;"><strong>Name</strong></font>
>     </td> </tr> <tr bgcolor="#FFFFFF">
>     <td width="20">&nbsp;</td>
>     <td>
>         <font style="font-family: sans-serif; font-size:12px;">Steve</font>
>     </td> </tr>

谢谢

【问题讨论】:

  • xpath 很适合我。我看不出它为什么不工作的理由。你能多显示一点代码吗?
  • 我发现当我使用完整格式的 HTML 文档时,我的代码确实匹配。感谢您指出我的工作方向是正确的。

标签: xpath html-agility-pack


【解决方案1】:

bgcolor 很奇怪,我发现使用 contains 可以解决问题。

这会起作用的......

SelectNodes(//*//tr[contains(@bgcolor, 'FFFFFF')]/td")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多