What Is XPath?

What Is XPath?

XPath is a language used for finding data in XML documents by parsing (scanning) those XML documents for specific values. XPath performs parsing of XML documents by applying an expression to the text of an XML document. The effective result is that an XPath expression allows navigation through XML document elements and attributes, retrieving items and values that match the expression passed into the XML document by XPath.

XPath performs a number of tasks:

  • Defines parts of an XML document using XPath syntax.

  • Utilizes path expressions to navigate through XML documents. The expression allows selection of nodes or sets of nodes from an XML document.

  • Uses numerous built-in functions to facilitate the processing of the preceding tasks. Built-in functions include functionality covering specific data types, including strings, numbers, dates, and times. Also, node and node sequences can be manipulated, among numerous other functions.

Three logical models have been developed by the W3C for representation of XML documents. These are the XML Document Object Model (XML DOM), XPath, and XML Infoset (XML Information Set). The XML DOM in Chapter 2 is generally relatively slow with respect to performance when compared with XPath. XML Infoset (http://www.w3.org/TR/xml-infoset) will not be covered in this book in favor of the more up-to-date XPath 2.0 and XQuery 1.0.

Absolute and Relative Paths

In its simplest form, an XPath expression forms an absolute or a relative path. An absolute path has a starting forward slash (/), and a relative path does not. An absolute path searches an XML document from the precise path specified. A relative path searches relatively (possibly within an XML document — not starting at the root node) from wherever specified. So, an absolute path searching from the root node looks something like this:


And a relative path looks something like this:

Previous Section
Next Section

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows