In XPath what is the difference between parent and ancestor?

问题: I am seeing 2 different axis in XPath parent ancestor Is ancestor[1] is equal to parent? i.e., //*[text()='target_text']//ancestor::div[1] is equal to...

问题:

I am seeing 2 different axis in XPath

  1. parent
  2. ancestor

Is ancestor[1] is equal to parent? i.e.,

//*[text()='target_text']//ancestor::div[1]

is equal to

//*[text()='target_text']//parent::div

回答1:

The difference between parent:: and ancestor:: axis is conveyed by their names: A parent is the immediately direct ancestor.

So, for this XML for example,

<a>
  <b>
    <c>
      <d/>
    </c>
  </b>
</a>
  • /a/b/c/d/parent::* selects c
  • /a/b/c/d/ancestor::* selects c, b, and a

So, yes /a/b/c/d/ancestor::*[1] would be the same as /a/b/c/d/parent::*.

  • 发表于 2019-02-28 23:32
  • 阅读 ( 154 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除