Search code examples
xpathhtmlunit

What is the difference between .// and // when they are used in the middle of the xpath?


What is the difference between using

htmlElement.getByXPath("//section")

and

htmlElement.getByXPath(".//section")

Do they both mean that the section element is searched only inside htmlElement? (and not in the entire xml starting at dom root)


Solution

  • The first //section will retrieve all <section> elements wherever they are in your document, and independently of the context.

    The second .//section will take into account the element you are currently located in (typically in an XSL transformation), so that it will retrieve only the <section> in the node represented by the htmlElement variable. I don't know htmlunit, but in XPath an equivalent would be descendant::section.