如何找到与条件匹配的元素,但没有与xpath匹配条件的后代?

时间:2014-10-15 16:31:19

标签: xpath

我想找到特定条件的最具体的匹配项。例如,给定以下HTML:

<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>

我想要一个xpath找到带有“我的狗点”文本的元素,然后给我一个带有div'first_child'和'second_child'的节点集,但是没有让我'first_parent'或'second_parent'或'爷爷”。此代码段中的ID用于说明,代码段的实际结构也是如此。要理解的重要一点是字符串可能在页面上出现多次,但我只想要包含它们的最具体的元素,而不是父元素。我意识到我会得到子元素(字体和字符),但我只想得到它们作为获得'first_child'和'second_child'的结果。我不想将它们作为结果集的一部分。

我试过了:

$x( "./descendant::*[ starts-with(normalize-space(string()), 'My dog spot') and not(starts-with(normalize-space(string(./descendant::*)), 'My dog spot'))]" )

但是当使用Firefox 15控制台(我正在使用的平台)运行时,它会获得html元素以及'first_child'和'second_child'。

0 个答案:

没有答案