XPath: what's the different between "begin with one slash" and "begin with 2 slashes"?

1.5k views Asked by At

I read some Xpath code, some begin with "/xxxx", some begin with "//xxxx". What're their differences? Do they have different behavior just in "Select" or different also in other behaviors?

I didn't find corresponding explanations on this site, any hints?

Thanks.

1

There are 1 answers

0
potame On

Beginning an XPath with one slash will retrieve the root of the document, so that /xxxx will match only the <xxxx> element that is the root of the XML.

Example:

<?xml version="1.0"?>
<xxxx> <!-- this one will match -->
  <level>
   <xxxx /> <!-- this one won't -->
  </level>
</xxxx>

Whereas //xxxx will match all <xxxx> elements anywhere in the document.

Example:

<?xml version="1.0"?>
<xxxx> <!-- this one will match -->
  <level>
   <xxxx /> <!-- this one will match as well -->
   <sublevel>
     <xxxx /> <!-- and also this one -->
    </sublevel>
  </level>
</xxxx>