6.2. XPath Support in Browsers
As XML grew in popularity, the need to access specific pieces of data contained within large amounts of code became apparent. In July 1999, XML Path Language (XPath) was introduced in the eXtensible Stylesheet Language (XSL) specification as a means to find any node within an XML document. XPath uses a non-XML syntax that closely resembles the path syntax of a file system, allowing the construction of paths to any part of a document. The language consists of location paths and other expressions, as well as a few helpful functions to aid in retrieving specific data.
6.2.1. Introduction to XPath
An XPath expression consists of two parts: a context node and a selection pattern. The context node is the context from which the selection pattern begins. Referring to books.xml from the previous section, consider this XPath expression:
book/author
If this expression were executed at the root level context, all <author/> nodes would be returned because the <book/> element is a child of the document element and contains an <author/> element. This expression is not very specific, so all <author/> elements are returned.
What if you want to retrieve only the <book/> element that has a specific ISBN? The XPath expression would look like this:
book[@isbn='9780470109496']
The book part of the expression describes which element to retrieve. Inside of the square brackets is a condition that this element must match. The @isbn part represents the isbn attribute (@
Get Professional Ajax, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.