XML documents are represented in a tree like structure with its elements representing parent and its child. This is clear in above example of contact details. The xml element contact act as a parent element and it has 7 child elements. Some of the documents may have even more child nodes or child node itself can have child nodes.
But above example represents only one contact information represented in the xml format. Ideally xml document will have lots of addresses within it in this format. If we have to fetch any of the address, then we might have to look into entire document. We cannot even predict the order of the data within the document. This will make fetching time consuming. Hence we need any easy way to fetch the data within xml document. This is done by using XPath. It gives the mechanism to find the data within XML document.
In XPath, using single line of code, one can traverse the data in the xml document. This single line of code is called Expression. These expressions are used to get the details from the documents rather than manual traversing of document. Let us elaborate our contact detail example to understand how expressions work.
Rose MathewAPT 201 Lakeside Village Drive Clinton Township MI US
586 656 898 122 Clinton Township 506 656 232 565 Clinton Township [email protected]
Above example contains the details about contact information about a person which has work address, phone numbers and email address. Now let us see how to traverse different nodes using XPath expressions.
Here Contact is the root node. It can be accessed by simply writing its name in the expression.
It will point to the root node now. Suppose we need to traverse the elements in the document. Then we have to specify the path to the element from root, by separating its child using ‘/’.
Contact / Phone
Contact/ Phone/ Number
When we traverse the elements from the root, we need to specify the full path to reach the element node. This method of traversing is called Absolute Location Path. There is another method of traversing called relative location paths. In this method, we can directly access the element nodes. When we directly access the element node without specifying the path, it will traverse all the element nodes irrespective of its level or path. In above example document, Town is the child element in both address and Phone. If we have to access the Town using absolute location path, then we have to write two lines of code starting from root node.
Contact/ Phone/ Town
But in relative location path method, if we simply specify Town, it will point to all the towns in the document, irrespective of its parent nodes.
It will access both the towns from address and phone. This method does not require knowing full path of any element and reduces the effort of writing multiline codes. This method can be used to find the child elements too.
Here parent nodes of the address are not specified. It searches for all the address/Town elements in the document. Same town can be accessed without specifying the parent nodes by using ‘//’ like below.
This will now search both towns from address and Phone. It specifies that any town which has ancestor as Contact. It is not considerate about the parent node of Town. This method also reduces the effort of writing lengthy codes or multiline codes.
An attribute of the element node can be pointed by using ‘@’ before the attribute name. But full path to reach that attribute should be given.
Contact/ [email protected]
Contact / [email protected]
Suppose we know the child element node and we have to select its parent node from the child node. This can be done by using ‘..’ (Double dots).
Here child element is selected using relative path and ‘..’ gives all the parent nodes of the element Phone and State.
Address element above has many child nodes. Suppose document has Address element for different users and we have to select all the child nodes of the address element for all the users. Then we have to write either absolute or relative path for the entire user which is time consuming and increases the length of the code. If we have some better option, then it will be easy to traverse all the nodes. This is achieved by using wild card ‘*’. It saves time and complexity of code.
This will select all the child elements under address. Suppose we need to select all the elements under Contact. Then
This will select name, address and its child elements, phone and its child elements, and email.
Suppose we have to combine two or more expression into a single line. This can be done by using pipe – ‘|’.
Contact/ Address/Town | Contact/ Phone/ Town
This will combine the towns from both address and phone. It acts like a union in SQL. Some other examples of using pipe is as shown below.
Contact/ Address/State | Contact/ Phone/ STD
Address/State | STD/..
In XPath we can even use predicate to compare the values and then show the result values. Suppose we have to display the address of people who are in MI.
Address[State = “MI”]
These are the examples shows how to select particular element from the document. This is similar to SELECT query with WHERE condition. Here the conditions are enclosed within the square brackets ‘’. One can have any condition within it. But the absolute of relative path of the element should be given followed by the condition inside ‘’.
Predicates can also be given on attributes in similar fashion, provided attributes are preceded by ‘@’ symbol to indicate that they are attributes.
These are the different ways of using predicates on attributes.
All these methods above use the XML way of retrieving the elements stored in the XML documents. Let us now see how query can be used to retrieve the elements.