on 04-10-2008 8:01 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
//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
/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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.