Tuesday, May 16, 2023

XPath Tutorial - How to select elements in XPATH based on attribute and element value example

In this XPATH tutorial we will see example of selecting elements based upon its value or attribute value. We will see how to select between two elements based upon value of its child elements or based upon value of its attribute. XPATH is an important concept to understand if you are working with XML files. Most of Back-office platform relies on XML files for transporting data from one system to other and if you are working on any back-office or middle office system in Java or .NET, its important to know about XPATH and be able to use XPATH to select data from XML. Some time back I have shared my XPATH notes for Java programmer and this example shows true power of XPATH as how convenient its to made selective decision.


For those who are completely new in XML and XPATH, XPATH is great xml tool which allows you to query XML document and select or retrieve selective data much like SQL but if you are new to XPATH or haven't had much experience with XML tool and technology than you will struggle to find correct syntax of XPATH for your different need.

XPath Tutorial - How to select elements in XPATH based on attribute and element value example




XPATH to select an element based on attribute and value

Here is our sample xml, which we will use in this XPATH tutorial :



<orders>
        <order orderNumber="1">
                <item>Electronics</item>
                <total>100</total>
        </order>
        <order orderNumber="2">
                <item>Computers</item>
                <total>200</total>
        </order>
        <order orderNumber="3">
                <item>food</item>
                <total>300</total>
        </order>
</orders>

XPATH example Tutorial to select element using attribute and value from XML
In the first example of XPATH expression, we will see how to select a total element for the order which has the item value "Electronics"

XPATH :
/orders/order[item/text()='Electronics']/total
value : 100

here text() function returns value of <item> element
[] brackets are used to define conditions or predicate, which is comparing text of <item> tag with “Electronics”. 

Similarly, we can extend this XPATH to find the total for Orders where the item is Computers and food

/orders/order[item/text()='Computers']/total
200

/orders/order[item/text()='food']/total
300

On the second example of XPATH expression, we will find the total element for an order whose orderNumber attribute has value "2"

XPATH : /orders/order[@orderNumber='1']/total
value : 100

here [] is used for condition and @ is used to get value from attribute.

/orders/order[@orderNumber='2']/total
200

/orders/order[@orderNumber='3']/total
300

If you are new to XML or XPATH I suggest using XML-SPY tool it has in built XPATH evaluator which shows the result of XPATH as soon as you type and it supports XPath1.0 and XPATH2.0 as beta. It also has XPATH copy functionality so you can just select any element in XML in its grid view and copy its XPATH to use in your Java program.


Other Java and XML tutorials you may like

3 comments :

Anonymous said...

XPath is indeed worth learning for any XML Java developers. By using XPath you can retrieve any element from XML documents, just like SQL.

Zeshan Ahmed said...

you nailed it man!

javin paul said...

@Zeshan, thank you very mcuh. Glad that you find this XPath tutorial in Java useful.

Post a Comment