Xquery的初步学习(一次Lab作业的总结)

Task 1: Open countries.xml, compose the following XQueries:

1. Return the area of Mongolia.

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

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

4. Returnthenamesofallcountrieswhereover50%ofthepopulationspeaksGerman. (Hint: Dependingonyoursolution, you may want to use ".", which refers to the "current element" within an XPath expression.)

5. Returnthenameofthecountrywiththehighestpopulation. (Hint: Youmayneedtoexplicitlycastpopulationnumbers as integers with xs:int() to get the correct answer.)

第一问:

for $x in doc("countries.xml")/countries/country
where $x/@name="Mongolia"
return <area>{data($x/@area)}</area>

第二问:

for $x in doc("countries.xml")/countries/country
for $y in $x/city
where $y/name= $x/@name
return $y/name 

第三问:

avg(doc("countries.xml")/countries/country[language="Russian"]/@population)

第四问:

for $i in (doc("countries.xml")/countries/country/language[@percentage>50 and .="German"]/../@name)
        return <name>{data($i)}</name>

第五问:

for $x in doc("countries.xml")/countries/country
where $x/@population = max(doc("countries.xml")/countries/country/@population)
return <name>{data($x/@name)}</name>
原文地址:https://www.cnblogs.com/northernmashiro/p/9080390.html