本文介绍了XPath 中 .//的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道绝对 XPath 会从 XML 树中的根节点返回检查过的节点.

但我无法理解 XPath 中用于检查/查找节点的 .// 的含义.

解决方案

. 是当前节点;它是 self::node() 的缩写.

// 是descendant-or-self 轴;它是 /descendant-or-self::node()/ 的缩写.

一起,.// 将从当前节点开始沿descendent-or-self 轴进行选择.将此与从文档根开始的 // 对比.

示例

考虑以下 HTML:

<身体><div id="id1"><p>第一段</p><div><p>第二段</p>

<p>第三段</p></html>

//p 将选择所有段落:

 

第一段

<p>第二段</p><p>第三段</p>

另一方面,如果当前节点位于 div 元素(具有 "id1"@id),则 .//p 将只选择当前节点下的段落:

 

第一段

<p>第二段</p>

注意当当前节点为id1 div.//p没有选中第三段,因为第三段是不在那个 div 元素之下.

I know absolute XPath will return the inspected node from root node in XML tree.

But I am not able to understand the meaning of .// used in XPath to inspect/find a node.

解决方案

. is the current node; it is short for self::node().

// is the descendant-or-self axis; it is short for /descendant-or-self::node()/.

Together, .// will select along the descendent-or-self axis starting from the current node. Contrast this with // which starts at the document root.

Example

Consider the following HTML:

<html>
  <body>
    <div id="id1">
      <p>First paragraph</p>
      <div>
        <p>Second paragraph</p>
      </div>
    </div>
    <p>Third paragraph</p>
  </body>
</html>

//p will select all paragraphs:

      <p>First paragraph</p>
      <p>Second paragraph</p>
      <p>Third paragraph</p>

On the other hand, if the current node is at the div element (with @id of "id1"), then .//p will select only the paragraphs under the current node:

      <p>First paragraph</p>
      <p>Second paragraph</p>

Notice that the third paragraph is not selected by .//p when the current node is the id1 div because the third paragraph is not under that div element.

这篇关于XPath 中 .//的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:08