[http://creativecommons.org/licenses/by/3.0/]
This work is licensed under a CC
Attribution 3.0 Unported License [http://creativecommons.org/licenses/by/3.0/]
XML and relational databases are not entirely different things: they share the basic model of providing support for storing and processing (potentially large amounts of) data. SQL/XML is an approach to bridge the two worlds of relational and XML databases, by allowing relational database to produce XML, and even to store and query XML. In the second half of this lecture we are taking a second look at XQuery, specifically its processing model and the way how queries are specifying the input documents to XQuery expressions.
query the XHTML snippet
persistent DOM)
flavors
XML
)
XML
type
SELECT e.EmpId, e.FirstName, e.LastName, e.StartDate, e.EndDate FROM Employees e WHERE e.EmpId = 12
SELECT XMLELEMENT(NAME "employee", XMLATTRIBUTES(e.EmpId as "id"), XMLELEMENT(NAME "names", XMLELEMENT(NAME "first", e.FirstName), XMLELEMENT(NAME "last", e.LastName)), XMLELEMENT(NAME "hire-dates", XMLATTRIBUTES(e.StartDate as "start", e.EndDate as "end"))) FROM Employees e WHERE e.EmpId = 12
no tables
declare namespace foo = "http://bar";
declare option xhive:queryplan-debug "true";
xml, xmlns, fn, xs, xsi, op, xdt, local
xhive
)
doc()
[http://www.w3.org/TR/xpath-functions/#func-doc] or collection()
[http://www.w3.org/TR/xpath-functions/#func-collection] specify input
declare default element namespace "http://www.topografix.com/GPX/1/1"; declare namespace saxon="http://saxon.sf.net/"; declare option saxon:output "method=text"; declare variable $dir := '/Users/dret/Desktop/Dropbox/training/'; declare variable $file := '2010-*.gpx'; declare variable $files := collection(concat($dir,'?select=',$file)); declare variable $lon := -122.48; declare variable $lat := 37.82; declare variable $box := 0.01; declare variable $lonlower := $lon - $box; declare variable $lonupper := $lon + $box; declare variable $latlower := $lat - $box; declare variable $latupper := $lat + $box; ( "Searching", count($files), "files with", count($files/gpx/trk/trkseg/trkpt), "track points:
", for $activity in $files where exists($activity/gpx/trk/trkseg/trkpt[ (@lon > $lonlower) and (@lon < $lonupper) and (@lat > $latlower) and (@lat < $latupper) ]) return ( saxon:format-date(xs:date(substring($activity/gpx/metadata/time/text(),1,10)), "[MNn] [D], [Y]"), ": ", $activity/gpx/trk/name/text(), "
") )
declare default element namespace "http://www.topografix.com/GPX/1/1"; declare variable $files := doc("/"); declare variable $lon := -122.48; declare variable $lat := 37.82; declare variable $box := 0.01; declare variable $lonlower := $lon - $box; declare variable $lonupper := $lon + $box; declare variable $latlower := $lat - $box; declare variable $latupper := $lat + $box; ( "Searching", count($files), "files with", count($files/gpx/trk/trkseg/trkpt), "track points:
", for $activity in $files where exists($activity/gpx/trk/trkseg/trkpt[ (@lon > $lonlower) and (@lon < $lonupper) and (@lat > $latlower) and (@lat < $latupper) ]) return ( substring($activity/gpx/metadata/time/text(),1,10), ": ", $activity/gpx/trk/name/text(), "
") )
doc()
and collection()
are synonymous
doc('foo.xml')
: relative path within xDB
doc('/bar/foo.xml')
: absolute path within xDB
doc('http://www.example.com/foo.xml')
: HTTP request
doc('xhive://foo/bar/../test.xml')
: complete xDB URI
doc('file:///my/doc.xml')
: absolute local file URI
doc('file:doc.xml')
: relative file URI
doc('lib/')
) results in
<a>123</a>, <b>456</b>
doc("books.xml")//author, doc("books.xml")//title
for $b in doc("books.xml")//book return $b/title, $b/author
for $b in doc("books.xml")//book return ( $b/title, $b/author )
declare boundary-space strip; let $a := "Bob Glushko" return <book> <title>Document Engineering</title> <author> { $a } </author> </book>
<book> <title>Document Engineering</title> <author>Bob Glushko</author> <price currency="USD">29.99</price> </book>
element "book" { element "title" { "Document Engineering" }, element "author" { "Bob Glushko" }, element "price" { attribute "currency" { "USD" }, 29.99 } }