(: SECTION 4 -- Saxon , XML and XPath The following examples use the mondial.xml file and the mondial.dtd DTD We use saxon to run and xmllint for formatting the results (xmllint --format) :) (: What are the names of all the countries? this is simple specification of the exact path :) { doc("mondial.xml")/mondial/country/name } (: What does this do? :) { doc("mondial.xml")/mondial/./country/name } (: What about this? :) { doc("mondial.xml")/mondial/country/../country/name } (: The construct // matches a country at any depth of the XML tree :) { doc("mondial.xml")//country } (: For the country with car code equal to "D" (Germany), find the neighboring countries Here we use the XPath predicate [@car_code="D"] and also the construct @, which refers to attributes of the XML nodes :) { doc("mondial.xml")//country[@car_code="D"]/border } (: Find the names of the countries with population at least 10 million The construct text() returns the text content of the node :) { doc("mondial.xml")//country[population/text() >= 10000000]/name } (: Which cities have population at least 4 million? :) { doc("mondial.xml")//city[population >= 4000000]/name } (: Find the name of the cities for the countries that are encompassed by europe:) { doc("mondial.xml")//country[encompassed/@continent="europe"]//city/name } (: Which rivers have source at the nortern hemisphere? :) { doc("mondial.xml")//river[source/latitude > 0.0]/name } (: Which are the rivers which start at Austria? Notice here that the country is not referenced by its name inside the river tag, but by its car code :) { doc("mondial.xml")//river[source/@country = (//country[name='Austria']/@car_code)]/name} (: Which countries are encompassed by both Asia and Europe ? We can write this query in two ways :) (: This uses two predicates. One will be evaluated first and then the other:) { doc("mondial.xml")//country[encompassed/@continent="asia"][encompassed/@continent="europe"]/name } (: This uses the construct "and" and uses a single predicate:) { doc("mondial.xml")//country[encompassed/@continent='europe' and encompassed/@continent='asia']/name } (: Which are the countries that neighbor France and have either a population more then 20 million or a total GDP over 10000? :) { doc("mondial.xml")//country[border/@country = (//country[name='France']/@car_code)] [population > 20000000 or gdp_total > 10000]/name } (: Which countries have at least 90% muslim population? :) (: The following XPath query does NOT work! This finds the countries that have muslims and some religion that has percentage at least 90% :) { doc("mondial.xml")//country[religions[@percentage >= 90] = 'Muslim']/name } (: Instead, we use nested predicates :) { doc("mondial.xml")//country[religions[@percentage >= 90] = 'Muslim']/name }