Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
932 views
in Technique[技术] by (71.8m points)

xml - What is meaning of .// in XPath?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...