-- Queries related to lecture on November 15, 2011
To use these examples, either install eXist (and enable its examples), or go to the XQuery sandbox (only available within ITU):
http://exist.itu.dk:8080/exist/sandbox/sandbox.xql
XML files used can be found at:
http://itu.dk/people/pagh/idb11/exml/
doc("acm-small.xml")
doc("acm-small.xml")//node[@label="Arrays"]
doc("acm-small.xml")//node[@label="Arrays"]/..
doc("acm-small.xml")//node[@label="Arrays"]/ancestor::*
doc("acm-small.xml")//node[@label="Arrays"]/ancestor::node
doc("acm-small.xml")//node[@label="Arrays"]/ancestor::node/@label/string()
doc("acm-small.xml")//node[@label="Arrays"]/../*/@label/string()
-- Show titles:
declare default element namespace "http://www.brics.dk/ixwt/recipes";
doc("recipes.xml")//title
-- Show ingredient names:
declare default element namespace "http://www.brics.dk/ixwt/recipes";
doc("recipes.xml")//ingredient/@name
-- Note: The computed result is not shown in the eXist web interface because it is not XML (no enclosing tag for attributes).
-- Show ingredient name strings:
declare default element namespace "http://www.brics.dk/ixwt/recipes";
doc("recipes.xml")//ingredient/@name/string()
- Show ingredient number 40:
declare default element namespace "http://www.brics.dk/ixwt/recipes";
(doc("recipes.xml")//ingredient)[40]/@name/string()
-- Count ingredients:
declare default element namespace "http://www.brics.dk/ixwt/recipes";
for $r in doc("recipes.xml")//recipe
return {$r/title}{fn:count($r//ingredient)}
-- Count "basic" ingredients that do not have ingredients
declare default element namespace "http://www.brics.dk/ixwt/recipes";
for $r in doc("recipes.xml")//recipe
return {$r/title}{fn:count($r//ingredient[fn:not(ingredient)])}
Working with XML in MySQL
Note: ExtractValue() returns only CDATA, and does not return any tags that might be contained within a matching tag, nor any of their content.
select id,ExtractValue(descr,'//rcp:title') from recipelist;
select id,ExtractValue(descr,'//rcp:step[1]') from recipelist;