XPath Tutorial

Getting started XPath tutorial, XPath is a address of node that are referring to a portions (XML code) in XML documents.

XPath Tutorial

  • Before you starting XSLT, you must have to know about how to traversing in XML using XPath syntax.

  • XPath give you control to traversing in any direction either forwards or backwards.

  • XPath address contain set of nodes for identifies node (bunch of code). XPath expression you can write either absolute full path or relative path.

Absolute Path Hierarchical structure are always referred from root path to a child node.

Relative Path Refer to "relative" to the current position. current position is describe the period ("."), and one step parent path is describe the double period ("..") it means file is locate in one step parent directory.

Getting Started XPath

Difference between / and // (Search Directives in XML)

/ Search directives

  • It's starts search selection from root element in document.

  • XPath expressions is like absolute path from the root element.

  • /empinfo When /empinfo is a absolute path from the root element. pretend as a root node.

  • You can use XPath search directives in middle of an XPath address to represent ancestor descendant relationships.

  • XPath addresses /empinfo/employee identifies employee elements. employee that is a next child (descendant) of empinfo element.

/empinfo/employee

<?xml version="1.0" standalone="yes"?>
<empinfo>
    <employee id="1">
        <name>Opal Kole</name>
        <designation discipline="web">Senior Engineer</designation>
        <email>[email protected]</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA">DBA Engineer</designation>
        <email>[email protected]</email>
    </employee>
</empinfo>

XPath expression select 3 to 12 line number employee node.


// Search directives

  • It's starts search selection anywhere in XML document.

  • XPath expressions is like relative path from the context node.

  • //empinfo When //empinfo define node path in anywhere in XML document. "//" find empinfo element locate any depth of XML document.

  • XPath addresses /empinfo//designation identifies any descendant designation element of element empinfo.

/empinfo//designation

<?xml version="1.0" standalone="yes"?>
<empinfo>
    <employee id="1">
        <name>Opal Kole</name>
        <designation discipline="web">Senior Engineer</designation>
        <email>[email protected]</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA">DBA Engineer</designation>
        <email>[email protected]</email>
    </employee>
</empinfo>

XPath expression select 5 and 10 line number designation node.


".//" (dot double forward slash) search directives identifies only descendant(child element) element. It's search relative path of current element.

.//name

<?xml version="1.0" standalone="yes"?>
<empinfo>
    <employee id="1">
        <name>Opal Kole</name>
        <designation discipline="web">Senior Engineer</designation>
        <email>[email protected]</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA">DBA Engineer</designation>
        <email>[email protected]</email>
    </employee>
</empinfo>

XPath expression select 4 and 9 line number name node.