0% found this document useful (0 votes)
23 views

Xpath: Supplier "Mother" Id "1"

XPath is a query language used to navigate elements and attributes in an XML document, analogous to SQL for databases. It uses path expressions to select nodes or node-sets in an XML tree based on relationships like parent-child and sibling. Some key features of XPath include the ability to retrieve elements, attributes, and text values; use predicates to filter results; perform basic functions; and select nodes relative to the current node.

Uploaded by

Anudeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Xpath: Supplier "Mother" Id "1"

XPath is a query language used to navigate elements and attributes in an XML document, analogous to SQL for databases. It uses path expressions to select nodes or node-sets in an XML tree based on relationships like parent-child and sibling. Some key features of XPath include the ability to retrieve elements, attributes, and text values; use predicates to filter results; perform basic functions; and select nodes relative to the current node.

Uploaded by

Anudeep
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

XPATH

XML documents can be thought of as a tree structure made up


parente,child and sibling relationships.
Xpath is to query the XML. It is like SQL to RDBMS
Xpath uses expressions to find elements, attributes and other
information.

<inventory>
<drink>

<lemonade supplier="mother" id="1">


<price>$2.50</price>
<amount>20</amount>
</lemonade>
<lemonade supplier="store" id="2">
<price>$5.50</price>
<amount>10</amount>
</lemonade>
<pop supplier="store" id="2">
<price>$1.50</price>
<amount>10</amount>
</pop>

</drink>
<snack>

<chips supplier="store" id="3">


<price>$4.50</price>
<amount>60</amount>
<calories>180</calories>
</chips>

</snack>
</inventory>

1.If we want to find the amount of lemonade stock


Inventory/drink/lemonade/amount

Drink is the child of inventory. Lemonade and pop are siblings. Price
and amount are children of lemonade.
2. If we want to find out the supplier of chips.
Inventory/snack/chips@supplier
3. We can also specify the above one like this
Inventory//chips@supplier
4. If we want to get all the prices then
Inventory//price
5. If we want to find the Price of First Lemonade
Inventory/drink/lemonade[1] --Predicates
6. Find the parent of lemonade
Inventory/drink/lemonade/..
7. Find all the children of pop
./inventory/drink/pop/* -- wild card
9. Find all the children in inventory(last generation)
inventory/*/*/*
10. Find all the inventory whose quantity > 15
inventory/*/*[amount >15]
inventory/*/*[amount >15]/..
11. Find lemonades whose quantity >15
inventory/drink/lemonade[amount >15]
inventory/drink/lemonade/amount[. > 15]/..
12. Find the drink whose lemonade quantity > 15
inventory/drink[lemonade/amount > 15] - you can write xpath
inside the [ ]
inventory/drink/lemonade/amount[. > 15]/../..
10. Find all the children of chips and pop( Pipe character to merge two
xmls)
inventory/snack/chips/* | inventory/drink/pop/*

11. XPath Functions


We can use the built in functions defined in Xpath
1. Position() gives the position of the node in the tree. Select
2nd lemonade
/inventory[1]/drink[1]/lemonade[position() = 2] is same as
/inventory[1]/drink[1]/lemonade[2]

2.
3.

4.
5.
6.
7.

If we want to find out first 2 lemonades


/inventory[1]/drink[1]/lemonade[position() < 3]
Last() gives the last node
/inventory[1]/drink[1]/lemonade[last()-1]
Node-name(node) gives the QName of the node
Node-name(/inventory[1]/drink[1]/lemonade) gives
{namespace}lemonade
Local-name(node) ignores the name space and gives the
node name
Number(14.5) 14.5
Round(4.5) 4
String(4.5) 4.15
Concat(Amount for lemonade
=,/inventory[1]/drink[1]/lemonade[2]/amount/text()) =
Amount for lemonade =10

8. String-join((welcome,to,soa), ) = Welcome to soa


String-join(welcome,to,soa)=welcomtosoa
9. Substring(InnovativeInfoTech,1,10)=Innovative
10. String-length(Innovative)=10
11.

You might also like