XPath is used to navigate in XML documents and it uses the expressions for navigation. Today I am going to discuss some key points about navigation(traversing) in XML Xpath.
Forward Traversing: We can traverse forward in the XML XPath in the following ways.
/node, //node, /node1/node2 , /node1//node2, //node1//node2, etc
/node:-> When we use / at the beginning of the expression it will define an absolute path to the node relative to root. It will find the node only at the root.
For example if we use /div, then it will traverse from element <div class=”node1″>
//node:-> When we use // at the beginning of the expression it will define relative path anywhere in the XML document. In the above XML, if we use //div it will find all the div elements in the XML document.
/node/node:->/ can be used in the middle of the path to define parent-child relationship. When we use / it defines a path that is the direct child of the parent node. For example, in the above XML if we use /div/div it will find the <div class=”node2″> by traversing from <div class=”node1″>
/node1//node2 :-> // can also be used in the middle of the path to define the parent-child relationship. When we use // it defines a path that can be the any child of the parent. For example in the above XML if we /div//div it will find all the child divs of the <div class=”node1″>
//node1//node2:-> This will define a path from any node and can define any of its child. For example in the example if we use //div//div, it will define a path from any div and will define any child div inside it.
Backward Traversing: There are the cases when we need to traverse backward in the XML Xpath. IN the XPath we can traverse backward using the double dots(..). We can traverse backward as many levels as we want like ../../… For example, if we want to find the parent of any child div selector then we can use it as.
Syntax for XML XPath:
<xpath expr="some expression" position="any value">
The “expr ” is any valid XPath expression e.g, /div/span//p
The possible values in the position can be inside, replace, after, before, attributes
inside: Used when we need to put some XML inside a Node.
replace: Used when we need to replace some XML with some other XML.
after: Used when we need to put some XML after a Node.
before: Used when we need to put some XML before a Node.
attributes: Used when we need to add some attributes to a Node.
That is it.!!!
In the next blog, I will discuss the XML XPath expression selectors.
If you liked this post, It would be very grateful if you write your opinions, comments and suggestions to keep the post updated and interesting.
Current Product Version - 1.0.0
Supported Framework Version - xml