Solved

XPath first node

Posted on 2006-06-21
3
514 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:aquasw
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16951707
Hi aquasw,
> string(/bookstore/book/title[@lang="eng"])

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


Cheers!
0
 

Author Comment

by:aquasw
ID: 16951864
I used this option, but get the error message "Too many items - string"
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 16952546
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mongodb hierarchy 2 55
Viewing XML as a table on a Mac 3 131
XSLT XML 4 19
write xml in vb.net 2 24
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question