//p[not(ancestor::*[3])]
//table[ancestor::*[1][self::p] or ancestor::*[2][self::p]]
tr/td//a[ancestor::*[1`][self::td] or ancestor::*[2][self::td]]
最佳答案
// # from the root node, look at all descendants
p[ # select nodes of type <p>, who have…
not(ancestor::*[3]) # …no ancestor 3 levels up
] #
// # from these nodes, select descendants
table[ # of type <table>, who have…
ancestor::*[1][self::p] # …a <p> as their direct ancestor
or # or
ancestor::*[2][self::p] # …a <p> as their second ancestor
] #
# syntax error, this should be a location step
tr # …select all nodes of type <tr>
/ # from their children…
td # …select all nodes of type <td>
// # from their descendants…
a[ # …select all nodes of type <a>, who have
ancestor::*[1][self::td] # …a <td> as their direct ancestor
or # or
ancestor::*[2][self::td] # …a <td> as their second ancestor
]
或者,用 HTML 表示:
<html>
<body>
<p>
<table>
<tr>
<td>
<a title="These would be selected." />
</td>
</tr>
</table>
</p>
</body>
</html>
无论如何,整个 XPath 没有太大意义。不言而喻<p><table>
HTML 无效。
关于xpath - 这个xpath是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4571169/