• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML certification Success - XPath examples

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Hari,

In page 16, of XML certification success part2, question 2:

<!-- books.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="BK1">
<name>Understanding XML</name>
<Author>Whizlabs</Author>
<price>50</price>
</book>
<book id="BK2">
<name>Understanding Java</name>
<Author>Whizlabs</Author>
<price>40</price>
</book>
<book id="BK3">
<name>XML Certification Made Easy</name>
<Author>Whizlabs</Author>
<price>75</price>
</book>
</books>

Which of the following XPath expressions will select the price element in
the third book element?
A. //book[position() = last()]/price
B. //book[@id ='BK3']/price
C. //book[position() = 3]/price
D. /books/book[position() = 3]/price
E. //price[position = last()]
F. //book/price[position = 3]

All are correct choices according to the article.

But I feel E and F are not.
Reason there is nothing called 'position', it should be 'position()'.

I tried the same on XML spy, it does not yield the correct result even with position().

//price[position() = last()], gives me all 3 prices
//book/price[position() = 3], no result was yielded.

Please correct me if I am wrong.
 
Ranch Hand
Posts: 578
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vasim,
You are right. Choices E anf F are not incorrect
and its not position. It should have been position()
Here are the reasons:

E. //price[position()=last()]

This gets in all the price elemnts and groups them by the containing child element. It will be like this

and when asked for a specific position, it goes through each of the branch and gets the last element !and so returns 50, 40 and 75


If our xml structure was like the one shown below

<!-- books.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="BK1">
<name>Understanding XML</name>
<Author>Whizlabs</Author>
<price>50</price>
</book>
<book id="BK2">
<name>Understanding Java</name>
<Author>Whizlabs</Author>
<price>40</price>
<price>30</price>
<price>90</price>
</book>
<book id="BK3">
<name>XML Certification Made Easy</name>
<Author>Whizlabs</Author>
<price>75</price>
</book>
</books>

the structure will be

then //price[position()=last()] will return 50,90 and 75

F. //book/price[position()= 3]
This tries to get the the 3 price elemnt of every book descendant element and not the 3 price descendant element of teh document as mentioned.

It should have been
//book/price[text()=75]

Thanks for pointing out the errors and sorry for the errors

I will make sure that they are rectifed as soon as possible

Good luck for the exam

[ June 21, 2005: Message edited by: Hari Vignesh Padmanaban ]
[ June 21, 2005: Message edited by: Hari Vignesh Padmanaban ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic