top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between “/” and “//” in Xpath?

+1 vote
1,403 views
What is the difference between “/” and “//” in Xpath?
posted Oct 8, 2016 by Jdk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Getting started XPath tutorial, XPath is a address of node that are referring to a portions (XML code) in XML documents.
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.

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>OpalKole@myemail.com</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA">DBA Engineer</designation>
        <email>maxmiller@email.com</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>OpalKole@myemail.com</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA">DBA Engineer</designation>
        <email>maxmiller@email.com</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>OpalKole@myemail.com</email>
    </employee>
    <employee id="2">
        <name from="CA">Max Miller</name>
        <designation discipline="DBA">DBA Engineer</designation>
        <email>maxmiller@email.com</email>
    </employee>
</empinfo> 

XPath expression select 4 and 9 line number name node.

answer Oct 18, 2016 by Manikandan J
...