Xpath Tutorial - How To Lead Elements Inwards Xpath Based On Attribute As Well As Chemical Part Value Example

In this XPATH tutorial nosotros volition encounter instance of selecting elements based upon its value or attribute value. We volition encounter how to direct betwixt ii elements based upon value of its tiddler elements or based upon value of its attribute. XPATH is an of import concept to empathise if y'all are working amongst XML files. Most of Back-office platform relies on XML files for transporting information from ane organisation to other together with if y'all are working on whatever back-office or middle business office organisation inwards Java or .NET, its of import to know well-nigh XPATH together with last able to utilization XPATH to direct information from XML. Some fourth dimension dorsum I bring shared my XPATH notes for Java programmer together with this instance shows truthful ability of XPATH every bit how convenient its to made selective decision. For those who are completely novel inwards XML together with XPATH, XPATH is corking xml tool which allows y'all to question XML document together with direct or recollect selective information much similar SQL but if y'all are novel to XPATH or haven't had much sense amongst XML tool together with applied scientific discipline than y'all volition scrap to honour right syntax of XPATH for your unlike need.

XPATH to direct chemical subdivision based on attribute together with value

Here is our sample xml, which nosotros volition utilization 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>

In this XPATH tutorial nosotros volition encounter instance of selecting elements based upon its value or a XPath Tutorial - How to direct elements inwards XPATH based on attribute together with chemical subdivision value example
In commencement instance of XPATH appear nosotros volition encounter how to direct full chemical subdivision for gild which has special value "Electronics"

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

here text() role 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 move extend this XPATH to honour full for Orders where special is Computers together with food

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

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

On minute instance of XPATH appear nosotros volition honour full chemical subdivision for gild whose orderNumber attribute has value "2"

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

here [] is used for status together with @ is used to larn value from attribute.

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

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

If y'all are novel to XML or XPATH I propose using XML-SPY tool it has inwards built XPATH evaluator which shows the upshot of XPATH every bit shortly every bit y'all type together with it supports XPath1.0 together with XPATH2.0 every bit beta. It also has XPATH re-create functionality together with then y'all tin move simply direct whatever chemical subdivision inwards XML inwards its grid stance together with re-create its XPATH to utilization inwards your Java program.

Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API amongst Spring Boot
Difference betwixt DOM together with SAX parser inwards Java

Belum ada Komentar untuk "Xpath Tutorial - How To Lead Elements Inwards Xpath Based On Attribute As Well As Chemical Part Value Example"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel