Introduction to XPath
XPath (XML Path Language) is a language used to address parts of an XML document. XPath models an XML document as a tree. XPath locator syntax uses core functions based on the node hierarchy of a document, and evaluates expressions to determine a location object.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document SYSTEM "http://commons.omniupdate.com/dtd/standard.dtd">
<meta name="author" content="John Doe" />
Every XML document can be represented in a structural form called tree. A XML tree originates from a single point, called root. All the child elements that follow the root can be referred to as branches or nodes. An XML document tree can be divided into smaller tree in such a way that any node can be considered the root of its own subtree. The following is the tree representation of the XML document above.
Based on the XML document and tree structure, the following are examples of XPath expressions:
“content” and “p” node
Axis Selection in XPath
XPath can be used to select nodes based on relative paths. Axis specifies the tree relationship between the nodes selected by the location step and the context node. The following are the available axes:
Contains the child elements of the context node.
Contains the descendants of the context node, which is a child or a child of a child and so on.
Contains the context node and the descendants of the context node.
Contains all nodes in the same document as the context node that precedes the context node in document order, excluding any ancestors.
Contains all the preceding siblings of the context node; if the context node is an attribute node or namespace node.
Contains all nodes in the same document as the context node that follows the context node in document order, excluding any descendants.
Contains all the following siblings of the context node; if the context node is an attribute node or namespace node.
Contains the ancestors of the context node and all its parent nodes.
Contains the context node along with the ancestors of the context node and all its parent nodes.
@ is abbreviated syntax for attribute
// is abbreviated syntax for descendant or self
.. is abbreviated syntax for parent
. is abbreviated syntax for self
* specifies that the element can be of any name
 bracketed numbers indicate to select the n element (in this case the first element)