What is the Difference Between “/” and “//” in XPath?

Single Slash “/”: Single slash is used to create XPath with absolute path.

Double Slash “//”: Double slash is used to create XPath with the relative path.

In XPath, “/” and “//” are both used to navigate the XML or HTML document structure, but they have different meanings:

  1. “/” (Single Slash): It is used for selecting the immediate child of the current node. For example, if you have an XPath expression like /div, it will select all div elements that are direct children of the current node.Example:
    /html/body/div

    This selects the div element that is a direct child of the body element, which is itself a direct child of the html element.

  2. “//” (Double Slash): It is used for selecting nodes in the document from the current node that matches the selection, regardless of their location in the document. It is more flexible and can be used to locate elements at any level of the document hierarchy.Example:
    //div

    This selects all div elements in the document, regardless of their position or nesting level.

In summary, the main difference is that “/” selects the immediate child, while “//” selects nodes at any level in the hierarchy beneath the current node.