Solved

XPath first node

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 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