XML Tutorial

DTD Tutorial

XPath Tutorial



XPath Axes Examples and Node Test


13 Axes are in XPath specification. XPath Axes are represent to a relationship between context node or referred node. Let's start XPath Axes examples.

What is an Axes? : Thirteen (13) Axes defined in XPath that enable to searching of different node part in XML document from current context node or the root node.

XPath Axes select the nodes from the context node within document.

XPath Axes Examples and Node Test

XPath Axes

Following 13 axes define the current node to relative node-set.

AxesName  ::=  'self'
                |'child'
                | 'descendant'
                | 'descendant-or-self'
                | 'parent'
                | 'ancestor'
                | 'ancestor-or-self'
                | 'attribute'
                | 'following'
                | 'following-sibling'
                | 'preceding'
                | 'preceding-sibling'
                | 'namespace'
Axis Name Description
self Axes select the context node.
child Axes select child of the context node.
descendant Axes select all descendants of the context node, child in any level depth.
descendant-or-self Axes select all descendants of the context node, child in any level depth also select context node to itself.
parent Axes select the parent node of the context node.
ancestor Axes select all parent node of the context node until the root node.
ancestor-or-self Axes select all parent node of the context node until the root node. also select context node to itself.
attribute Axes select attributes of the context node.
following Axes select all nodes after the context node, excluding attributes node or namespaces node.
following-sibling Axes select all following sibling of the context node.
Axes select none, If context node is attributes node or namespace node following sibling empty.
preceding Axes select all nodes before the context node, excluding attributes node or namespace node.
preceding-sibling Axes select attributes of the context node.
following Axes select all following sibling of the context node.
Axes select none, If context node is attributes node or namespace node preceding sibling empty.
namespace Axes select all namespace node of the context node.

Syntax :

You can use any of the predefined XPath Axes and test with the any of the nodes within document.

AxesName::node[predicate]

Parameter :

  • predicate (optionally) specifies sequence of node enclosed to a [].
  • Axes name and node are separated by :: (two times colon).

Following xml document is our experimental document,

xpath_location_path.xml
<?xml version="1.0" standalone="yes"?>
<empinfo>
    <employee id="1">
        <name>Opal Kole</name>
        <designation discipline="web" experience="3 year">Senior Engineer</designation>
        <email>OpalKole@myemail.com</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA" experience="2 year">DBA Engineer</designation>
        <email>maxmiller@email.com</email>
    </employee>
    <employee id="3">
        <name>Beccaa Moss</name>
        <designation discipline="appdev">Application Developer</designation>
        <email>beccaamoss@email.com</email>
    </employee>
</empinfo>

Node Test

XPath node test is a part of XPath expression for finding nodes in XML document.

Select Axis Description
//name/self::* Select the name context node.
child::* Select All child nodes of the context node.
child::node() Select all child nodes of the context node.
child::empinfo Select all child elements of empinfo node.
//employee/descendant::* Select all descendant of the employee node.
//descendant::employee Select all descendant of the employee node in context node.
//employee/descendant-or-self::* Select all descendant of the employee nodes and context node itself.
//descendant-or-self::employee Select all descendant of employee node with context node itself.

Ancestor

//employee/ancestor::* Select all ancestor node of the employee node.
//ancestor::name Select all ancestor of the name node in context node.
//employee/ancestor-or-self::* Select all ancestor of the employee nodes and context node itself.
//name/ancestor-or-self::employee Select all ancestor of name node with context node itself.
//name/parent::* Select parent node of the name context node.
//name/parent::employee Return result node if employee node is parent node of the context node, otherwise no node found.
//attribute::id Select all node with id attribute.
//attribute::* Select all node with any attribute.
//employee[@id=1]/following::* Select all nodes (with child nodes) after the context node.
//employee[@id=1]/following-sibling::* Select all sibling nodes after the context node.
//employee[@id=3]/preceding::* Select all nodes (with child nodes) before the context node.
//employee[@id=3]/preceding-sibling::* Select all sibling nodes before the context node.