XPath uses path expressions to select nodes or node sets in an XML document. An XPath location path has one or more location steps, each separated by ‘/’. The location path can be an absolute path or relative path.

Example of XPath syntax

<?xml version="1.0" encoding="UTF-8"?>

<data>

    <employee>       

        <office>

            <dept id="accounts">

            <name>Raju</name>

            <age>50<age>

            </dept>

        </office>

      </employee>  

</data>

Absolute location path begins with the root node

For XML document given, the absolute path is:

/data/employee/…

Relative location path begins from current folder the path is defined from

For XML document given, the relative path from employee node can be:

employee/office/dept…

Each step is defined as:

axis-name::node-test[predicate]*

where,

  • axis-name – is the path between selected nodes and current nodes.
  • node-test – is the node within an axis.
  • predicate – extract the selected node-set. It can contain literal values, operators and other XPath expressions.

The syntax to access elements, attributes are given in following tutorials.

 

›› go to examples ››