An XPath quick reference

This is a very brief quick reference for those of you who know what XPath is, but don’t use it often enough to have it ingrained in your heads, like myself.

XPath Examples

//person Returns a NodeSet containing ALL XML tags in the document with the name ‘person’, no matter where they are in the document.
//person[@firstname=’patrick’] Returns a NodeSet containing ALL XML tags in the document with the name ‘person’ and an attribute ‘firstname’ with a value of ‘patrick’, no matter where they are in the document.
/person/@firstname Returns the value of the attribute ‘firstname’ for all direct children with a name of ‘person’.
/animal[contains(string(),’rabbit’)] Returns all ‘animal’ tags that have a string value containing the string ‘rabbit’.

I highly recommend a site out there that allows you to test your XPath’s right on the site: http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm