Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...