In this blog I’ll show you some of the useful Xpath expression which we use during development. I will start with one simple xml to make you you understand in simplest way.
Hope It will help those who are new in xpath.
PurchaseOrder.xml
1 >All items
$body/orac:PurchaseOrder/orac:items/orac:item
Result:
<p:item
id="GF234324" xmlns:p="http://www.oracle.com">
<p:description>Denim
Jeans</p:description>
<p:department>Clothing</p:department>
<p:price>30.99</p:price>
<p:quantity>2</p:quantity>
</p:item><p:item
id="HD312782" xmlns:p="http://www.oracle.com">
<p:description>iPod 80Gb
White</p:description>
<p:department>Electrical</p:department>
<p:price>99.99</p:price>
<p:quantity>1</p:quantity>
</p:item><p:item
id="HD998775" xmlns:p="http://www.oracle.com">
<p:description>iPod
Headphones</p:description>
<p:department>Electrical</p:department>
<p:price>19.99</p:price>
<p:quantity>1</p:quantity>
</p:item><p:item
id="KD123984" xmlns:p="http://www.oracle.com">
<p:description>Frying Pan</p:description>
<p:department>Home</p:department>
<p:price>9.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
2> The first item in
the order
$body/orac:PurchaseOrder/orac:items/orac:item[1]
We are
using the [1] to select the first item – XPath is unlike most programming
languages which index arrays from 0
rather than 1
Result:
<p:item
id="GF234324" xmlns:p="http://www.oracle.com">
<p:description>Denim
Jeans</p:description>
<p:department>Clothing</p:department>
<p:price>30.99</p:price>
<p:quantity>2</p:quantity>
</p:item>
3>The id of the first
item in the order
$body/orac:PurchaseOrder/orac:items/orac:item[1]/@id
Result:GF234324
4>The description
whose id is HD312782.
·
$body/orac:PurchaseOrder/orac:items/orac:item[@id="HD312782"]/orac:description
<p:description
xmlns:p="http://www.oracle.com"> iPod 80Gb White
</p:description>
·
$body/orac:PurchaseOrder/orac:items/orac:item[@id="HD312782"]/orac:description/text()
iPod 80Gb White
·
If with same id="HD312782"
having more than one items,then it will result all the items
$body/orac:PurchaseOrder/orac:items/orac:item[@id="HD312782"]
Result:
<p:item id="HD312782"
xmlns:p="http://www.oracle.com">
<p:description>iPod 80Gb White</p:description>
<p:department>Electrical</p:department>
<p:price>99.99</p:price>
<p:quantity>1</p:quantity>
</p:item><p:item
id="HD312782" xmlns:p="http://www.oracle.com">
<p:description>iPod Headphones</p:description>
<p:department>Electrical</p:department>
<p:price>19.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
$body/orac:PurchaseOrder/orac:items/orac:item[@id="HD312782"]/orac:description/text()
Result:
iPod 80Gb WhiteiPod Headphones
5> All items whose
department having electrical in the purchase order
·
$body/orac:PurchaseOrder/orac:items/orac:item[orac:department="Electrical"]
Result:
<p:item
id="HD312782" xmlns:p="http://www.oracle.com">
<p:description>iPod 80Gb
White</p:description>
<p:department>Electrical</p:department>
<p:price>99.99</p:price>
<p:quantity>1</p:quantity>
</p:item><p:item
id="HD998775" xmlns:p="http://www.oracle.com">
<p:description>iPod
Headphones</p:description>
<p:department>Electrical</p:department>
<p:price>19.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
·
$body/orac:PurchaseOrder/orac:items/orac:item[orac:department="Electrical"]/orac:price
<p:price
xmlns:p="http://www.oracle.com">99.99</p:price>
<p:price
xmlns:p="http://www.oracle.com">19.99</p:price>
6> The second item whose
department having electrical in the purchase order
$body/orac:PurchaseOrder/orac:items/orac:item[orac:department="Electrical"][2]
Result:It
will show electrical items 2nd
<p:item
id="HD998775" xmlns:p="http://www.oracle.com">
<p:description>iPod
Headphones</p:description>
<p:department>Electrical</p:department>
<p:price>19.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
7> showing all dept
$body/orac:PurchaseOrder/orac:items/orac:item/orac:department/text()
ClothingElectricalElectricalHome
8>The
last item in the purchase order
$body/orac:PurchaseOrder/orac:items/orac:item[last()]
Result:
<p:item
id="KD123984" xmlns:p="http://www.oracle.com">
<p:description>Frying
Pan</p:description>
<p:department>Home</p:department>
<p:price>9.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
9>The
second item in the purchase order
·
$body/pur:PurchaseOrder/pur:items/pur:item[position()=2]
<p:item
id="HD312782" xmlns:p="http://www.oracle.com">
<p:description>iPod 80Gb
White</p:description>
<p:department>Electrical</p:department>
<p:price>99.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
or
data($body/*:PurchaseOrder/*:items/*:item[2]/*:description)
Result: iPod 80Gb White
or $body/*:PurchaseOrder/*:items/*:item[2]
<p:item
id="HD312782" xmlns:p="http://www.oracle.com">
<p:description>iPod 80Gb
White</p:description>
<p:department>Electrical</p:department>
<p:price>99.99</p:price>
<p:quantity>1</p:quantity>
</p:item>
I
want to retrieve price name of 2nd item node using relative path
$body//*:item[2]/*:price/text()
Result:99.99
I
want to retrieve all price name using
relative path
$body//*:price/text()
Result:
30.9999.9919.999.99
10> Relative path
·
The card number (assuming you don’t know
its path)
$body//orac:cardNumber/text()
Result:1234-5678-1234-5678
·
The card number (assuming you don’t know
its path or namespace):
$body//*:cardNumber/text()
Result:1234-5678-1234-5678
·
know the namespace
$body/orac:PurchaseOrder/orac:cardDetails/orac:cardNumber/text()
Result:1234-5678-1234-5678
11>The
purchase order date in UK date format (i.e. DD/MM/YYYY)
translate('AB/CD/EFGH','EFGH-CD-AB',
xs:string($body/orac:PurchaseOrder/orac:date/text()))
Result:
02/04/2010
No comments:
Post a Comment