In my daily work as frontend developer at www.bleau.dk I do a lot of XSLT “programming” – XPath 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 operator. 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.