Help! Beginner in Xquery : basic questions.

II’m a very naive user of Xquery, who is just at the beginning of learning. :slight_smile:

I have plein of problems fwith syntax. I try two explain two of them here.

I’m using the Exist sample database “mondial.xml” :

http://demo.exist-db.org/rest//db/mondial/mondial.xml

When I write the query :

doc/mondial/country/border[@country=“F”]

to know the code of countries bordering France, I get too muvh information
(the entire country elements), while I want juste the country codes.
Of course, I tried to use a FLWOR construction, but, there, I get a message of syntax error that
I cannot even understant.

More generally : how to “point” to values of XML attributes, so as to reriebve just them ?

By the way of the syntax of FLOWR : I managed to get the countrie having more than 60000000 habitants simply by writing :

for $p in doc(“mondial.xml”)/mondial/country let $b:=$p/population where
$b >6000000 return $b.

but it’s a mystery to me why my construction works there and that does not work, for, istance, in my trial to improve the first query.
:evil:

Thanks a lot, and sorry for the naivety of my questions. I’m try to learn by myself.

If you want to just return the attrbutes you could use:


for $c in input()/mondial/country/border[@country="F"]
return $c/@country

To recast the attribute as an element you can use:


for $c in input()/mondial/country/border[@country="F"]
return <C>{string($c/@country)}</C>

To return elements showing the countries that border France, you might use:

for $p in input()/mondial/country
let $b := $p/border/@country
where $b = "F"
return <bordering>{$p/@car_code}</bordering>

and to get the country name with the code:

for $p in input()/mondial/country/border
where $p/@country = "F"
return <bordering country="{$p/../@car_code}">{$p/../name}</bordering>

Your second construct returns the node “population” - that is all that is contained in the variable $b.