to the main page about the tutorial  THE XML REVOLUTION  -  TECHNOLOGIES FOR THE FUTURE WEB back up next

Examples

The following XQuery expressions extract information from the recipe collection:

"The titles of all recipes":
for $t in document("recipes.xml")//title
return $t
<title>Beef Parmesan with Garlic Angel Hair Pasta</title>,
<title>Ricotta Pie</title>,   
<title>Linguine Pescadoro</title>,   
<title>Zuppa Inglese</title>,   
<title>Cailles en Sarcophages</title> 

"The dishes that contain flour":
<floury>
  { for $r in document("recipes.xml")//recipe[.//ingredient[@name="flour"]]
    return <dish>{$r/title/text()}</dish>
  }
</floury>
<floury>   
  <dish>Ricotta Pie</dish>   
  <dish>Zuppa Inglese</dish>   
  <dish>Cailles en Sarcophages</dish> 
</floury> 

"For each ingredient, the recipes that it is used in":
for $i in distinct-values(document("recipes.xml")//ingredient/@name)
return <ingredient name="{$i}">
             { for $r in document("recipes.xml")//recipe
               where $r//ingredient[@name=$i]
               return $r/title
             }
       </ingredient>
<ingredient name="beef cube steak">
  <title>Beef Parmesan with Garlic Angel Hair Pasta</title>   
</ingredient>,   
<ingredient name="onion, sliced into thin rings">
  <title>Beef Parmesan with Garlic Angel Hair Pasta</title>   
</ingredient>,   
  ...
<ingredient name="butter">
  <title>Beef Parmesan with Garlic Angel Hair Pasta</title>
  <title>Cailles en Sarcophages</title>
</ingredient>,
  ...

"The recipes that use some of the stuff in our refrigerator":
distinct-values(
  for $r in document("recipes.xml")//recipe
  for $i in $r//ingredient/@name
  for $j in document("fridge.xml")//stuff[text()=$i]
  return $r/title
)
<title>Beef Parmesan with Garlic Angel Hair Pasta</title>,
<title>Ricotta Pie</title>,
<title>Linguine Pescadoro</title>,

back COPYRIGHT © 2000-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH next