XPath first node

I'm using xpath to analyze xml file.

this is my xml:

<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>


I use the following xpath:
string(/bookstore/book/title[@lang="eng"])

it fails becouse I have more then one node of that type. How do I ask for the first node?
Thanks.
aquaswAsked:
Who is Participating?
 
Geert BormansInformation ArchitectCommented:
aquasw,

do you have multiple bookstore or book elements?

then you need
string(/bookstore[1]/book[1]/title[@lang="eng"])

it is likely that you don't need to cast to a string
since it will only work if the title is string content
0
 
Geert BormansInformation ArchitectCommented:
Hi aquasw,
> string(/bookstore/book/title[@lang="eng"])

use
string(/bookstore/book/title[@lang="eng"][1])


Cheers!
0
 
aquaswAuthor Commented:
I used this option, but get the error message "Too many items - string"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.