Solved

XPath first node

Posted on 2006-06-21
3
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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