轴导航期间的xpath上下文节点



查看DTD:

<!ELEMENT root (a|b)+>
<!ELEMENT a (a|b)*>
<!ELEMENT b (a|b)*>

以及以下简单的XML:(为了方便起见,我标记了这些元素)

<root>
  <b1>
    <b2></b2>
    <a1></a1>
  </b1>
  <b3></b3>
</root>

当我执行以下查询时:

a) /descendant-or-self::node()/b[1]
b) /descendant-or-self::b[1]

在a)和b)中,在应用[1]之前,我得到了XML树中的所有b
但当我要求第一个b时,在a)中我得到{b1,b2},在b)中我只得到{b1}
我的问题是,在这个场景中,上下文节点的逻辑是什么?换言之,为什么"/decendance or self::node()/b"(我知道它相当于"//")和"/dicendance或self/b"之间有区别?

/descendant-or-self::node()/b[1]选择作为其父元素的第一个子([1]b的所有b元素,因为/descendant-or-self::node()/b[1]/descendant-or-self::node()/child::b[1]的缩写。/descendant-or-self::b选择文档中的所有b元素,并使用/descendant-or-self::b[1]选择其中的第一个元素。

最新更新