Using XPath operator “//” aka “descendant-or-self” in XSLT

In my daily work as frontend developer at www.bleau.dk I do a lot of “programming” – is one of the things you need to master most. With the focus mainly set on closing tasks I have not had the time to test a – well nice to know thing with the “” XPath . Tonight I finally found out what had been a question in my mind for at long period:

The “//” (decendant-or-self) XPath operator: Can it be used relative inside a full XPath? Will it “search” from the Document top, or start where it is placed inside the XPath selector?

An example XML:

<?xml version="1.0" encoding="utf-8"?>

<people>
  <group type="frontEnd">
    <person name="Sten" />
  </group>
  <group type="backend">
    <person name="Sten" />
  </group>
</people>

The two XPaths

//person[@name='Sten'] – Returns a nodeset containing 2 elements

//group[@type="frontEnd"]//person[@name='Sten'] – Returns a nodeset containing only 1 element

So as you can see the first one looked from the root (/People) and anywhere inside the document. The second one had the “//” inside a XPath, which in made the “//” operator look from the spot it was and below.

Conclusion

Well, I just got a litle wiser as I learned that I actually can use the “//” operator inside XPath selectors.

Links
US

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>