Xpath 查询以查找包含某个后代的元素 [英] Xpath query to find elements which contain a certain descendant
问题描述
我正在使用 Html Agility Pack 在网页上运行 xpath 查询.我想在表中找到包含某个有趣元素的行.在下面的示例中,我想获取第二行.
I'm using Html Agility Pack to run xpath queries on a web page. I want to find the rows in a table which contain a certain interesting element. In the example below, I want to fetch the second row.
<table name="important">
<tr>
<td>Stuff I'm NOT interested in</td>
</tr>
<tr>
<td>Stuff I'm interested in</td>
<td><interestingtag/></td>
<td>More stuff I'm interested in</td>
</tr>
<tr>
<td>Stuff I'm NOT interested in</td>
</tr>
<tr>
<td>Stuff I'm NOT interested in</td>
</tr>
</table>
我想做这样的事情:
//table[@name='important']/tr[has a descendant named interestingtag]
除非使用有效的 xpath 语法.;-)
Except with valid xpath syntax. ;-)
我想我可以找到有趣的元素本身,然后从返回的节点沿着父链向上移动,但似乎应该有一种方法可以一步完成,而我只是密集.
I suppose I could just find the interesting element itself and then work my way up the parent chain from the node that's returned, but it seemed like there ought to be a way to do this in one step and I'm just being dense.
推荐答案
"has a后代名为 interestintag" 在 XPath 中拼写为 .//interestintag
,因此您要查找的表达式为:
"has a descendant named interestintag" is spelled .//interestintag
in XPath, so the expression you are looking for is:
//table[@name='important']/tr[.//interestingtag]
这篇关于Xpath 查询以查找包含某个后代的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!