What is XPath?
- A W3C Standard which stands for “XML Path Language”.
- A relatively simple syntax for pointing to (addressing) parts of XML documents.
- Designed for use in URLs as fragment identifiers (the part after the “#”).
- XPath uses a directory path style syntax to specify XML elements: /book/section/title.
- Defines a library of standard functions for use in addresses.
- Used by XSLT to match style rules to elements and perform other selection tasks.
- Used by XLink to create links among XML elements (through the XPointer recommendation).
Benefits:
Application Development
- Provides a standard syntax by which XML processors can access XML elements.
- Provides a standard addressing syntax for use within XML documents to do linking and addressing.
How XPath Works:
The purpose of XPath is to provide a standard string-based syntax by which components of XML documents can be addressed with precision. XPaths are primarily used within XML documents to specify pointers as parts of hypertext links. For example, the following XLink uses an XPath (technically, an XPointer) to address the second chapter of a target document:
<xlink:simple href="somedoc.xml# //book/chapter[2] "> Second Chapter</xlink:simple>
An XPath is a series of steps, much like a directory path, where each step names an element or other type of XML construct. The XPath in the above example can be read as “starting with the root of the document (' // '), find the first 'book' element (' book ') then find the second chapter element (' /chapter[2] ').” The “ [2] ” is called a predicate and specifies additional rules for selecting an element. Thus, the string “/chapter[2]” means “from all the chapter elements below 'book', select the second one.” Predicates can be any XPath expression, which may include things like string matches and external functions.
XPath views XML documents as trees of elements (formally, “nodes”), so the path in the XPath is defining a path down the tree. The kinds of things that can be addressed with XPath pointers include elements, attributes, processing instructions, and text sequences. In the abstract, an XPath is said to point to a “node set” such that the result of resolving an XPath pointer is a set of zero or more nodes.
Many XML support libraries include XPath support, making it easy for XML processing applications to resolve XPaths, especially in the context of DOM-based processing.
|