Tuesday, May 7, 2024
HomeJavaXPath Tutorial - The way to choose components in XPATH based mostly...

XPath Tutorial – The way to choose components in XPATH based mostly on attribute and factor worth instance


Disclosure: This text could comprise affiliate hyperlinks. If you buy, we could earn a small fee.

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

XPATH to pick a component based mostly on attribute and worth

Right here is our pattern xml, which we are going to use on this XPATH tutorial :

<orders>
        <order orderNumber=“1”>
                <merchandise>Electronics</merchandise>
                <whole>100</whole>
        </order>
        <order orderNumber=“2”>
                <merchandise>Computer systems</merchandise>
                <whole>200</whole>
        </order>
        <order orderNumber=“3”>
                <merchandise>meals</merchandise>
                <whole>300</whole>
        </order>
</orders>

XPATH example Tutorial to select element using attribute and value from XML

Within the first instance of XPATH expression, we are going to see tips on how to choose a complete factor for the order which has the merchandise worth “Electronics”

XPATH :

/orders/order[item/text()=’Electronics’]/whole

worth : 100

right here textual content() perform returns worth of <merchandise> factor

[] brackets are used to outline circumstances or
predicate, which is evaluating textual content of <merchandise> tag with “Electronics”. 

Equally, we will lengthen this XPATH to search out the full for Orders the place the merchandise is
Computer systems and meals

/orders/order[item/text()=’Computers’]/whole

200

/orders/order[item/text()=’food’]/whole

300

On the second instance of XPATH expression, we are going to discover the full factor for an order whose orderNumber attribute has worth “2”

XPATH : /orders/order[@orderNumber=”1″]/whole

worth : 100

right here [] is used for situation and @ is used to
get worth from attribute.

/orders/order[@orderNumber=”2″]/whole

200

/orders/order[@orderNumber=”3″]/whole

300

If you’re new to XML or XPATH I counsel utilizing XML-SPY software it has in
constructed XPATH evaluator which reveals the results of XPATH as quickly as you sort and
it helps XPath1.0 and XPATH2.0 as beta. It additionally has XPATH copy performance
so you’ll be able to simply choose any factor in XML in its grid view and replica its XPATH
to make use of in your Java program.

Different Java and XML tutorials you might like



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments