Xpath Tutorial - How To Guide Elements Inwards Xpath Based On Attribute In Addition To Chemical Part Value Example
Saturday, June 16, 2018
Add Comment
In this XPATH tutorial nosotros volition run across instance of selecting elements based upon its value or attribute value. We volition run across how to choose betwixt 2 elements based upon value of its tike elements or based upon value of its attribute. XPATH is an of import concept to empathise if you lot are working amongst XML files. Most of Back-office platform relies on XML files for transporting information from 1 arrangement to other in addition to if you lot are working on whatever back-office or middle business office arrangement inwards Java or .NET, its of import to know near XPATH in addition to move able to utilisation XPATH to choose information from XML. Some fourth dimension dorsum I bring shared my XPATH notes for Java programmer in addition to this instance shows truthful ability of XPATH every bit how convenient its to made selective decision. For those who are completely novel inwards XML in addition to XPATH, XPATH is corking xml tool which allows you lot to enquiry XML document in addition to choose or think selective information much similar SQL but if you lot are novel to XPATH or haven't had much sense amongst XML tool in addition to applied scientific discipline than you lot volition grapple to detect right syntax of XPATH for your dissimilar need.
XPATH to choose chemical portion based on attribute in addition to value
Here is our sample xml, which nosotros volition utilisation inwards 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>
<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>
In starting fourth dimension instance of XPATH human face nosotros volition run across how to choose full chemical portion for gild which has detail value "Electronics"
XPATH :
/orders/order[item/text()='Electronics']/total
value : 100
here text() business office returns value of <item> element
[] brackets are used to define status or predicate, which is comparison text of <item> tag amongst “Electronics”. Similarly nosotros tin terminate extend this XPATH to detect full for Orders where detail is Computers in addition to food
/orders/order[item/text()='Computers']/total
200
/orders/order[item/text()='food']/total
300
On minute instance of XPATH human face nosotros volition detect full chemical portion for gild whose orderNumber attribute has value "2"
XPATH : /orders/order[@orderNumber='1']/total
value : 100
here [] is used for status in addition to @ is used to larn value from attribute.
/orders/order[@orderNumber='2']/total
200
/orders/order[@orderNumber='3']/total
300
If you lot are novel to XML or XPATH I advise using XML-SPY tool it has inwards built XPATH evaluator which shows the trial of XPATH every bit presently every bit you lot type in addition to it supports XPath1.0 in addition to XPATH2.0 every bit beta. It also has XPATH re-create functionality in addition to thus you lot tin terminate simply choose whatever chemical portion inwards XML inwards its grid persuasion in addition to re-create its XPATH to utilisation inwards your Java program.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services in addition to REST API amongst Spring Boot
Difference betwixt DOM in addition to SAX parser inwards Java
0 Response to "Xpath Tutorial - How To Guide Elements Inwards Xpath Based On Attribute In Addition To Chemical Part Value Example"
Post a Comment