Rob
Rob

Reputation: 15992

XQuery : get only one XML element among many with the same name

I'm using this URL to get information of a given location (with latitude/longitude) :

http://maps.googleapis.com/maps/api/geocode/xml?latlng=48.845909,2.341762&sensor=true

What I want to do is to get the basic address of this place (address, town). So I tried this XQuery request :

let $lat := 48.845909,
$lng := 2.341762,
$url := concat('http://maps.googleapis.com/maps/api/geocode/xml?latlng=',$lat,',',$lng,'&sensor=true'),
$resp := doc($url)
return $resp//result/formatted_address/string()

But with this I get all the formatted_address elements of the XML result, so here I get :

12 Rue Royer-Collard, 75005 Paris, France
Val-de-Grâce, Paris, France
75005 Paris, France
5th arrondissement of Paris, Paris, France
Paris, France
Paris, France
Île-de-France, France
France

And I only want the first line. What should I do ?

Upvotes: 0

Views: 466

Answers (1)

BeniBela
BeniBela

Reputation: 16917

Simply use [1] to get the first element.:

let $lat := 48.845909,
$lng := 2.341762,
$url := concat('http://maps.googleapis.com/maps/api/geocode/xml?latlng=',$lat,',',$lng,'&sensor=true'),
$resp := doc($url)
return ($resp//result/formatted_address/string())[1]

Or move it to the first result (probably faster):

let $lat := 48.845909,
$lng := 2.341762,
$url := concat('http://maps.googleapis.com/maps/api/geocode/xml?latlng=',$lat,',',$lng,'&sensor=true'),
$resp := doc($url)
return $resp//result[1]/formatted_address/string()

Upvotes: 1

Related Questions