如何找到与条件匹配但没有与xpath条件匹配的后代的元素?
我想为特定条件找到最具体的匹配项。例如,给定以下HTML:如何找到与条件匹配但没有与xpath条件匹配的后代的元素?,xpath,Xpath,我想为特定条件找到最具体的匹配项。例如,给定以下HTML: <div id="grandpa"> <div id="first_parent"> <div id="first_child"> <font>My</font> <br> <font>dog</font> <br> <font>spot&
<div id="grandpa">
<div id="first_parent">
<div id="first_child">
<font>My</font>
<br>
<font>dog</font>
<br>
<font>spot</font>
</div>
</div>
<div id="second_parent">
<div id="second_child">
<font>My</font>
<br>
<font>dog</font>
<br>
<font>spot</font>
</div>
</div>
</div>
但是当使用Firefox 15控制台(我正在使用的平台)运行时,它会得到html元素以及“first_child”和“second_child”。那么,您希望结果集是
,
而不包含子节点吗?我认为这在XPath中是不可能的,因为它通常返回XML的节点。所有非叶节点仍包含其子节点。但是,通常调用XPath的语言允许您在需要时删除子节点。我意识到返回的节点将包含它们的子节点,这很好。我不想让他们自己进入结果集中。结果集的长度应为2。我修改了查询以反映这一点。嗯,当我对输入运行XPath时(将
更改为
),它只返回两个元素。如果您在Firefox 15中运行它,并保持brs与我的相同,您将得到3个元素。我将更新问题,使其更准确。
$x( "./descendant::*[ starts-with(normalize-space(string()), 'My dog spot') and not(starts-with(normalize-space(string(./descendant::*)), 'My dog spot'))]" )