Saturday, October 27, 2018

Double Slash ' // ' Before Single Slash ' / ' In XPath

Double Slash ' // ' only selects all the nodes that are descendants of the matching nodes including the matching nodes themselves. Single Slash ' / ' only selects all the nodes that are the child of the matching nodes.

XPath having Double Slash ' // ' before Single Slash ' / ' is given below:
//div/span
For Example:

<div id='div_one'>
   <span id='span_one' />
   <div id='div_two'>
      <span id='span_two' />
   </div>
</div>

Here, If we use '//div/span' expression then we will get two 'span' nodes.

Why?
  • For '//div' we will get two div node (div_one & div_two) because Double Slash ' // ' only selects all the nodes that are descendants of the matching nodes including the matching nodes themselves.
  • For '/span' we will get two span node (span_one & span_two) as span_one is the direct child of div_one & span_two is the direct child of div_two.
Another Example:

<div id='div_one'>
   <ul>
      <li>
         <span id='span_one' />
      </li>
   </ul>
</div>
<div id='div_two'>
   <p>
      <span id='span_two' />
   </p>
</div>

Here, If we use '//div/span' expression then we will not be able to get any result as 'span' is not the direct child of any of the above 'div' nodes.

No comments:

Post a Comment