BASICS

ADMINISTRATION

DEVELOPMENT

SUPER ADMIN

  LAST UPDATED
5/22/2013

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">
<document>
      <config>
            <meta name="author" content="John Doe" />
      </config>
      <title>
            New Page
      </title>
      <content>
            <p>Hello World</p>
      </content>
</document>

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.

XML Tree Structure

Based on the XML document and tree structure, the following are examples of XPath expressions:

XPath Expression

/document/title

Result

title node

XPath Expression

/document/content

Result

“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:

XPath Axes

child

Contains the child elements of the context node.

descendant

Contains the descendants of the context node, which is a child or a child of a child and so on.

descendant-or-self

Contains the context node and the descendants of the context node.

preceding

Contains all nodes in the same document as the context node that precedes the context node in document order, excluding any ancestors.

preceding-siblings

Contains all the preceding siblings of the context node; if the context node is an attribute node or namespace node.

following

Contains all nodes in the same document as the context node that follows the context node in document order, excluding any descendants.

following-siblings

Contains all the following siblings of the context node; if the context node is an attribute node or namespace node.

ancestor

Contains the ancestors of the context node and all its parent nodes.

ancestor-or-self

Contains the context node along with the ancestors of the context node and all its parent nodes.

Additionally:

@ 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

[1] bracketed numbers indicate to select the n element (in this case the first element)

 

XSL ElementsXPath Functions

 


Was this page helpful?

Additional Comments:


(Include your email address if you would like a reply)