Sample Solutions for the XPath Exercises of Lab 2 ================================================= These are possible ways to express the queries of Lab 2. There are also many equivalent solutions. 1. Return the area of Mongolia. string(//country[@name="Mongolia"]/@area) string(/descendant::country[@name="Mongolia"]/@area) 2. Return the names of all countries with population greater than 100 million. //country[@population>100000000]/@name 3. Return the names of all countries where over 50% of the population speaks German. //country[language[text()="German"][@percentage>50]]/@name 4. Return the names of all countries where a city in that country contains more than one-third of the country's population. //country[@population < 3*city/population]/@name 5. Return the population density of Qatar. Note: Since the quotes/ operator has its own meaning in XPath, the division operator is infix div. So to compute population density use “(@population div @area)”. //country[@name="Qatar"]/@population div //country[@name="Qatar"]/@area 6. Return the names of all countries whose population is less than one thousandth that of some city (in any country). //country[@population < 0.001*//city/population]/@name 7. Return the names of all cities that have the same name as the country in which they are located. //city[name = parent::country/@name]/name 8. Return all city names that appear more than once, i.e., there is more than one city with that name. You may return a city name multiple times if it simplifies your query. (Hint: You might want to use the “preceding” and/or “following” navigation axes for this query.) //city[name = parent::country/following::city/name]/name 9. Return the names of all countries which have a city such that some other country has a city of the same name. //country[city/name = (preceding::city/name | following::city/name)] 10. Return the number of countries where Russian is spoken. count(//country[language = "Russian"]) 11. Return the names of all countries that have at least three cities with population greater than 3 million. //country[count(city[population > 3000000]) > 2]/@name 12. Return the names of all countries for which the data does not include any languages or cities, but the country has more than 10 million people. //country[not(language) and not(city)][@population > 10000000] 13. Return the names of all countries whose name textually contains a language spoken in that country. For instance, Uzbek is spoken in Uzbekistan, so return Uzbekistan. //country[language[contains(../@name,.)]] 14. Return the names of all countries in which people speak a language whose name textually contains the name of the country. For instance, Japanese is spoken in Japan, so return Japan. //country[language][contains(language,@name)] 15. Return all languages spoken in a country whose name textually contains the language name. For instance, German is spoken in Germany, so return German. //country[language][contains(@name,language)]/language 16. Return all languages whose name textually contains the name of a country in which the language is spoken. For instance, Icelandic is spoken in Iceland, so return Icelandic. //country/language[contains(self::*,parent::*/@name)] //country/language[contains(.,../@name)]