Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "//" and "/" in XPath?

I was trying my hands on XPath for python-selenium.

I used this link for trying some XPaths' from tutorials:

So I tried these two variants of XPaths'.

  1. This expression

    //webengagedata//preceding-sibling::*
    

returned 14 results

  1. And this expression

    //webengagedata/preceding-sibling::*
    

returned 9 results

What does the "//" do to match 5 more results?

like image 432
Prasanth Avatar asked Mar 29 '17 17:03

Prasanth


People also ask

What is the meaning of '/' in XPath?

Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

What is meaning of single slash and double slash in XPath?

A double slash " // " means any descendant node of the current node in the HTML tree which matches the locator. A single slash " / " means a node which is a direct child of the current.

What is the difference between XPath and XPath?

The absolute xpath has the complete path beginning from the root to the element which we want to identify. An absolute xpath starts with the / symbol. One drawback with the absolute xpath is that if there is any change in attributes beginning from the root to the element, our absolute xpath will become invalid.

What is the difference between and in XPath Mcq?

'/' in XPath signifies looking for the element immediately inside its parent element. Whereas '//' signifies to look for any child or any descendant element inside the parent element.


2 Answers

/ vs // in general

Both child (/) and descendant-or-self (//) are axes in XPath.

  • / is short for /child::node()/.

    Use / to select a node's immediate children.

  • // is short for /descendant-or-self::node()/.

    Use // to select a node, its children, its grandchildren, and so on recursively.


/ vs // with preceding-sibling::*

Your specific question asks about the difference between //preceding-sibling::* and /preceding-sibling::*.

Since your data is offsite and complex, let's consider instead this present and simpler XML:

<r>
  <a/>
  <b>
    <c/>
    <d/>
  </b>
</r>

For this XML,

  1. /r/preceding-sibling::* selects nothing because r has no preceding siblings.
  2. /r//preceding-sibling::* selects the preceding siblings elements of all of the descendant or self nodes of r. That is, a, b, c and d. (Remember, /r//preceding-sibling::* is short for /descendant-or-self::node()/preceding-sibling::*, not /descendant-or-self::*/preceding-sibling::*) Note that even though b and d are predecessor siblings to no elements, they are predecessor siblings to text nodes because the above XML has whitespace after b and d. If all whitespace were removed, then only a and c would be selected.
  3. /r/descendant::*/preceding-sibling::* selects the preceding sibling elements of all descendant elements of r. That is, a and c. Note that b and d are not selected because they are not preceding sibling elements to any descendant elements of r -- unlike the previous example, text nodes do not qualify.
like image 136
kjhughes Avatar answered Sep 19 '22 05:09

kjhughes


For your example

//webengagedata/preceding-sibling::* ---> returned 9 results

Because there are only 9 tags which are exact sibling of webengagedatatags thats why it is showing 9 records

//webengagedata//preceding-sibling::* ---> returned 14 results

Here it is considering child tags as well as biziclop said x/descendant-or-self::node()/y

like image 42
NarendraR Avatar answered Sep 22 '22 05:09

NarendraR