【问题标题】:Find all the <tr>s which have <a> which href contains string "xxx"?查找所有 <a> 的 <tr>,其中 href 包含字符串“xxx”?
【发布时间】:2013-12-14 00:15:53
【问题描述】:

如何使用xpath获取所有&lt;tr&gt;s,其中包含&lt;a&gt;,href包含字符串“xxx”?

.......
<tr><td>...</td><td><a href="xxx3" /a></td><td>...</td>......</tr> <!-- yes -->
<tr><td>...</td><td>...</td><td><a href="ab_xxx" /a></td>......</tr> <!-- yes -->
<tr><td>...</td><td>...</td><td>...</td>......</tr> <!-- no-->
.....

【问题讨论】:

    标签: xml xpath html-agility-pack xpath-1.0


    【解决方案1】:

    试试 xpath:

    //tr[.//a[contains(@href, 'xxx')]]
    

    几点说明:

    • 必须是.//a 而不是//a(注意起始时间)。该句点表示查看当前节点内的任何位置(即 tr)。如果没有句点,它会寻找出现在文档中任何位置的锚点。
    • contains方法用于检查href属性是否包含字符串。

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2023-01-31
      • 2016-03-03
      • 2012-06-28
      相关资源
      最近更新 更多