Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath doubt

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
i am studying from tutorial...


things are written there like...

Code:
<?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>




Path Expression Result
================ ==========
bookstore Selects all the child nodes of the bookstore element


/bookstore Selects the root element bookstore

whats the difference between two ?

is not it both of them will give

Code:
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>
?
 
PLZ answer my above question.

and also they say...


//book Selects all book elements no matter where they are in the document
bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element


but it seems both of them give same result...
 
That is a mistake. "bookstore" will not select all the child nodes of the bookstore element. For that, you'll need "bookstore/*".

"bookstore" and "/bookstore" will both give
Code:
<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>
The "/" means its an absolute path, so if the context node was the first "title" element for instance, "bookstore" would give nothing, but "/bookstore" would give the same as above.

"//book" will look at every node in the document and match any book element. "bookstore//book" will first set the context on the bookstore element, then search any nodes under that, which in this case is the same.



Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Hi sir, i could not get properly but i understnad the whole answer key lies in ur statement

"so if the context node was the first "title" element for instance, "bookstore" would give nothing, but "/bookstore" would give the same as above."

i dont understand what do u mean by context node in this case.
whats a context node ? i am sorry this may be a silly question..but i dont know. i am just a beginner. plz explain a little bit.
thanks
 
The context node is the node currently "selected". For instance, if you used the following XSL:
Code:
<xsl:for-each select="bookstore/book/title">
  ......
</xsl:for-each>
Before the for-each statement, the context was at the document root. Inside the for-each, a new context has been chosen by the select attribute. In this case, the context loops through all the nodes that satisfy "bookstore/book/title". So the first time through the loop, the context is on:
Code:
<title lang="eng">Harry Potter</title>
So, here "bookstore" will give you nothing, as its a relative path and there is no node "bookstore" beneath the "title" node. "/bookstore", however, is an absolute path, which starts from the document root (signified by "/"), and thus will return the bookstore node.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Sir, i am very much happy with your explanation. Its superb. i have understood it . things are clear.
many many thanks
 
may i ask one more question ?

i have noticed they(in the tutorial , the link i have given above) have not mentioned

"/bookstore/book" though they have mentioed
"bookstore/book"....... why did they not mentioned "/bookstore/book"? is it implicit( i guess) ? bcoz i could always opt for absolute path..is not it ?
 
Yes, you are right. They just have a few examples. Most of the time the context node will be the document root, so those two will be equivalent.

I suggest you try to do some simple XSL transformation, and it will become clear.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top