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 to select element based on attribute and value
Here is our sample xml, which we will use in this XPATH tutorial :
In first example of XPATH expression we will see how to select total element for order which has item value "Electronics"
value : 100
here text() function returns value of <item> element
 brackets are used to define condition or predicate, which is comparing text of <item> tag with “Electronics”. Similarly we can extend this XPATH to find total for Orders where item is Computers and food
On second example of XPATH expression we will find total element for 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.
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