How to Select Nodes in XPath using Xml
In this article I will explain how to select the nodes in XPath.
XPath uses path expressions to select nodes or node-sets or elements in the XML document. The node is selected by following a path or steps, the most useful path expression are listed below. Their are some expression and their description in the following listing.
- nodename : Select all the nodes that have the node name "nodename".
- / : Select from the root node element.
- // : Selects nodes in the document from the current node that we specify that match the selection no matter where they are located in the document.
- . : It is used to select the current node.
- .. : Used to select the parent of the current node.
- @ : It is used to select the attributes.
<?xml version="1.0" encoding="utf-8" ?>
Now the following lines shows that how the nodes of above example represents the path expressions.
Employee : Select all the nodes that have the node name "Employee".
/Employee : Select the root element Employee.
/Employee/Trainee : Select all the Trainee element that are children of Employee.
/Employee/Engineer : Select all the Engineer element that are children of Employee.
//Trainee : Selects all Trainee elements no matter where they are in the document, In the above example their are only one Trainee node ,if their are more than one then it select all these Trainee element in the document.Similary for //Engineer to select all the Engineer elements in the document.
Employee//Trainee : Selects all Trainee elements that are descendant of the Employee element, no matter where they are under the Employee.
//@street : Select all the attributes that are named as street.
Ask Your Question
Got a programming related question? You may want to post your question here
Programming Answers here