Solved

XPath first node

Posted on 2006-06-21
3
510 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now