CSE 344 section 06 solutions ================================================= 0. List the entire contents of Mondial. doc("mondial.xml")/mondial 1. Give a list of all the countries in XML. { doc("mondial.xml")//country } 2. Give a list of the countries that Germany borders. { doc("mondial.xml")//country[@car_code="D"]/border } 3. Give the names of all the countries with populaion at least 10 million. { doc("mondial.xml")//country[population/text() >= 10000000]/name } To do the comparison, you need to obtain the character string within each element. You do this by using the text() function of XPath as an immediate "child" of population/ . XPath will then coerce the string to a number automatically. Another way to write this query: { doc("mondial.xml")//country[population >= 10000000]/name } (If a element has only text, its name can be used without having to specifically use the text() function of XPath.) 4. Find all cities located in countries that are partially or fully part of Europe. (The cities themselves don't have to be in Europe.) { doc("mondial.xml")//country[encompassed/@continent="europe"]//city } Conditional expressions can have complex XPath expressions inside as well. Here we search for countries by matching an attribute of a 's subelement. 5. Find the names of all rivers that start north of the equator (at a positive latitude). { doc("mondial.xml")//river[source/latitude > 0.0]/name } 6. Find the names of all rivers that start in Iceland. { doc("mondial.xml")//river[source/@country = (//country[name='Iceland']/@car_code)]/name } Notice how we have nested one absolute XPath expression inside another - we compare the country attribute against the ID code of the country named Iceland. 7. Get the names of all countries in both Asia and Europe. { doc("mondial.xml")//country[encompassed/@continent='europe' and encompassed/@continent='asia']/name } How does this work ? In XPath, equality comparisons have implicit existential quantifiers. This means they return true if *one* of the items in the left-hand sequence matches *one* of the items in the right-hand sequence (either sequence can consist of just one item, such as 'europe' above). This is true of all comparison operators, actually. Hence, since there exists an subelement, and another distinct subelement, there can be a match. This would not work: { doc("mondial.xml")//country/encompassed[@continent='europe' and @continent='asia']/name } because here there really is only one item on each side of the equality test, there being only one "continent" attribute in an . Alternative way: { doc("mondial.xml")//country[encompassed/@continent='europe'][encompassed/@continent='asia']/name } XPath condition brackets stack from left to right. 8. Challenge problem: Get the name of every country that borders France *and* has either population greater than 20 million *or* GDP greater than 10000. { doc("mondial.xml")//country[border/@country = (//country[name='France']/@car_code)][population > 20000000 or gdp_total > 10000]/name }