XML World-Countries XPath and XQuery Exercises


XQuery
Question 1
Return the area of Mongolia.
for $c in doc("countries.xml")/countries/country
where $c/@name = "Mongolia"
return $c/data(@area)

Question 2
Return the names of all cities that have the same name as the country in which they are located.

for $c in doc("countries.xml")/countries/country
where $c/@name = $c/city/name
return   < name &gt {$c/data(@name)} < /name &gt

Question 3
Return the average population of Russian-speaking countries.

let $plist := doc("countries.xml")/countries/country[language="Russian"] 

 return  

 avg($plist/data(@population))


Question 4
Return the names of all countries that have at least three cities with population greater than 3 million.


for $c in doc("countries.xml")/countries/country
where count($c/city[population > 3000000])>=3
return $c/data(@name)
Question 5
Create a list of French-speaking and German-speaking countries. The result should take the form:
 Question 6
 
Return the countries with the highest and lowest population densities. Note that because the "/" operator has its own meaning in XPath and XQuery, the division operator is infix "div". To compute population density use "(@population div @area)". 
This is a tricky one. We make a list of all the densities and then we show the max and min of them. 
  

Σχόλια

Δημοφιλείς αναρτήσεις