xpath:按标签值查找所有祖先(查找具有单元格值的所有 xhtml 表)


xpath: Find all ancestor by tag value (find all xhtml tables that have cell value)

我有一个带有表格的 xhtml 页面。通常,其中一个在单元格中有文本"针文本"。如何获得这样的所有桌子?

<table class="content_table">
  <tr>
    <td>Column 1</td>
    <td>Column 2</td>
  </tr>
  <tr>
     <td>Value 11</td>
     <td>Value 21</td></tr>
  <tr>
    <td><a href="link.html">needle text</a></td>
    <td>Value 22</td>
  </tr>
</table>

注意:表格可以带有标签:thead,th等。

  1. 从 xhtml 页面收集所有表 - 用"//*[@class='content_table']"完成。
  2. 找到带有文本的节点,但没有成功。我试过
    //alink[text() = "needle text"]但得到空的结果。
  3. 在父树的父树的父级中查找表作为祖先。
    //alink[text() = "needle text"]/..

你可以试试下面的 XPATH

//a[.='needle text']/ancestor::table[1][@class='content_table']

//table[@class='content_table' and .//a[.='needle text']]