cancel
Showing results for 
Search instead for 
Did you mean: 

Xpath path expression?

Former Member
0 Kudos

Hi Gurus,

i've gone thru the XPath tutorial http://www.w3schools.com/xpath/xpath_syntax.asp

i've a small doubt in "selecting nodes" section. i'm unable to understand the diference between two expressions '/' and '//'. can anyone explain me with example or just send me results of the following XML document

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

</bookstore>

for the following expressions

bookstore

/bookstore

bookstore/book

//book

bookstore//book

//@lang

points will b rewarded

Thanks in advance

Faisal

Edited by: Abdul Faisal on Apr 10, 2008 9:02 AM

Edited by: Abdul Faisal on Apr 10, 2008 9:03 AM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi

check below (answers are in bold font)

for the following expressions

bookstore -

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

/bookstore - </bookstore>

bookstore/book -

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

//book

say , there is one more bookstore element with the name bookstore1 with the sttructure as follows

<bookstore1>

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

</bookstore1>.

Then //book will return all the book elements in the document including the book element under bookstore1.

regards

krishna

Former Member
0 Kudos

Thanks for wonderful masterpiece.

please give me the results for the following path expressions as well

/bookstore/*

//*

//title[@*]

//book/title | //book/price

//title | //price

/bookstore/book/title | //price

for the below XML Document

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

</bookstore>

Thanx

Faisal

Former Member
0 Kudos

Hi

check below

//*

selects the whole document ....

//title@* -

say for eg, there is one more title element as follows somewhere in the document

<title lenghth="5">Learning XML</title>, then the above expressin will return

<title lang="eng">Harry Potter</title>

<title lang="eng">Learning XML</title>

<title lenghth="5">Learning XML</title>

regards

krishna

Former Member
0 Kudos

/bookstore/*

<book>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

</book>

<book>

<title lang="eng">Learning XML</title>

<price>39.95</price>

</book>

regards

krishna

Former Member
0 Kudos

//book/title | //book/price

<title lang="eng">Harry Potter</title>

<price>29.99</price>

<title lang="eng">Learning XML</title>

<price>39.95</price>

regards

krishna

Former Member
0 Kudos

//title | //price

SAy for eg, there is one more node like book1 in the same xml doc as follows,

<book1>

<title lang="eng1">Learning XML1</title>

<price>49.95</price>

</book1>,

then the above exp will return

<title lang="eng1">Learning XML1</title>

<price>49.95</price>

<title lang="eng">Harry Potter</title>

<price>29.99</price>

<title lang="eng">Learning XML</title>

<price>39.95</price>

regards

krishna

Former Member
0 Kudos

/bookstore/book/title | //price

say there is another bookstore element , bookstore1 as follows

<bookstore1>

<book>

<title lang="eng">Learning XML</title>

<price>70</price>

</book>

</bookstore1>

then, the above exp will return

<title lang="eng">Harry Potter</title>

<title lang="eng">Learning XML</title>

<price>29.99</price>

<price>39.95</price>

<price>70</price>

regards

krishna

Former Member
0 Kudos

thanks

Answers (1)

Answers (1)

Former Member
0 Kudos

HI,

Context objects are a simple alternative to XPath expressions for accessing the contents of a message. Take the following message instance as an example:

<InvoiceOut>

<customerData>

<address>

<name> ... </name>

<postalCode> ...</postalCode>

...

</address>

...

</customerData>

</InvoiceOut>

To access the content of the <postalCode> field, you would use the following expression in XPath:

/InvoiceOut/customerData/address/postalCode

If you needed this expression in more than one condition, you would have to rewrite or copy it each time. Instead, you can assign a context object to the <postal Code> field, for example with the name postalCode. You can then use the postalCode context object in all conditions where you need the value of the <postalCode> field, which makes the conditions easier to read.

Comparison between XPath and Context Object

XPath--

/InvoiceOut/customerData/address/postalCode = “69120”

Context Object--

postalCode = “69120”

rEGRDS