topics in database systems: xquery
DESCRIPTION
Topics in Database Systems: XQuery. Jonathan Foss Dr. Alexandra I. Cristea http://www.dcs.warwick.ac.uk/~acristea/. Previously we looked at: XPath Namespaces Next: XQuery. XQuery http://www.w3.org/TR/xquery/. What is XQuery?. XQuery is the language for querying XML data - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/1.jpg)
Jonathan FossDr. Alexandra I. Cristea
http://www.dcs.warwick.ac.uk/~acristea/
Topics in Database Systems: XQuery
![Page 2: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/2.jpg)
2
• Previously we looked at:– XPath– Namespaces
• Next:– XQuery
![Page 4: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/4.jpg)
4
What is XQuery?• XQuery is the language for querying XML data • XQuery for XML is like SQL for databases • XQuery is built on XPath expressions • XQuery is defined by the W3C • XQuery is supported by all the major database
engines (IBM, Oracle, Microsoft, etc.) • XQuery is a W3C recommendation (Jan 2007)
thus a standard
![Page 5: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/5.jpg)
5
XQuery - Examples of Use
• Extract information to use in a Web Service • Generate summary reports • Transform XML data to XHTML • Search Web documents for relevant
information
![Page 6: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/6.jpg)
6
XQuery compared to XPath• XQuery 1.0 and XPath 2.0 share the same data
model and support the same functions and operators.
• XQuery 1.0 is a strict superset of XPath 2.0 XPath 2.0 expression is directly an XQuery 1.0
expression (a query)• The extra expressive power is the ability to:
– Join information from different sources and– Generate new XML fragments
![Page 7: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/7.jpg)
7
Xquery ‘compilers’
•Free 30 day trial download athttp://www.altova.com/download/xmlspy/xml_editor_enterprise.html•Syntax check at: http://www.w3.org/2007/01/applets/xqueryApplet.html •Other open source tools such as Xqilla•See http://www.w3.org/XML/Query/#implementations
![Page 8: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/8.jpg)
8
XQuery query makeup
• Prolog– Like XPath, XQuery expressions are evaluated
relatively to a context– explicitly provided by a prolog (header)~ header with definitions
• Body– The actual query
• Generate• Join• Select
![Page 9: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/9.jpg)
9
XQuery Ex.: Prolog + Query
![Page 10: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/10.jpg)
10
XQuery Prolog (i.e., header(s))• Settings define various parameters for the XQuery processor language,
such as:xquery version "1.0";module namespace math = "http://example.org/math-
functions"; (: module declaration: resource in lib :)declare base-uri "http://example.org";declare default element namespace
"http://example.org/names";declare namespace xs= "http://www.w3.org/2001/XMLSchema";import module "http://www.w3.org/2003/05/xpath-functions"
at "logo.xq";declare variable $x as xs:integer := 7;declare function addLogo($root as node()) as node()*{ };(: etc :)
![Page 11: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/11.jpg)
11
Body: Constructors
Direct constructors in Xquery:
<XMLfragment>my fragment </XMLfragment>
– Evaluates to the given XML fragment
![Page 12: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/12.jpg)
12
Explicit constructors
computed constructors
![Page 13: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/13.jpg)
13
Variable bindings (implicit constructors)
<employee empid="{$id}"> <name>{$name}</name>
{$job} <deptno>{$deptno}</deptno> <salary>{$SGMLspecialist+100000}</salary>
</employee>
![Page 14: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/14.jpg)
14
How to Select Nodes with XQuery?• Functions
– XQuery uses functions to extract data from XML documents.
• (X)Path Expressions– XQuery uses path expressions to navigate
through elements in an XML document.• Predicates
– XQuery uses predicates to limit the extracted data from XML documents.
![Page 15: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/15.jpg)
15
Functions• doc()
– function to open a file• Example:
– doc("books.xml")
• Note: A call to a function can appear where an expression may appear.
![Page 16: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/16.jpg)
16
Path Expressions
• Example:select all the title elements in the "books.xml"
file: doc("books.xml")/bookstore/book/title
![Page 17: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/17.jpg)
17
Predicates• Example:
select all the book elements under the bookstore element that have a price element with a value that is less than 30 :
doc("books.xml")/bookstore/book[price<30]
![Page 18: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/18.jpg)
18
At a glance: function, path, predicate
![Page 19: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/19.jpg)
19
FLWOR• For, Let, Where, Order by, Return
= main engine~ SQL syntax (SFW(GH)O)~ programs and function calls
![Page 20: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/20.jpg)
20
FLWOR by comparison with Path expressions
• select all the title elements under the book elements that are under the bookstore element that have a price element with
a value that is higher than 30.
• Path expression:doc("books.xml")/bookstore/book[price>30]/title
• FLWOR expression: for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title
![Page 21: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/21.jpg)
21
Sorting in FLWOR• for $x in doc("books.xml")/bookstore/book
where $x/price>30 order by $x/title return $x/title
![Page 22: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/22.jpg)
22
Present the Result In an HTML List<ul> { for $x in
doc("books.xml")/bookstore/book/title order by $x return <li>{$x}</li> } </ul>
![Page 23: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/23.jpg)
23
Result HTML List<ul> <li><title lang="en">Everyday
Italian</title></li> <li><title lang="en">Harry Potter</title></li> <li><title lang="en">Learning XML</title></li> <li><title lang="en">XQuery Kick
Start</title></li> </ul>
![Page 24: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/24.jpg)
24
Eliminate element (here: title)<ul> { for $x in doc("books.xml")/bookstore/book/title order by $x return <li>{data($x)}</li> (: also text{} :)} </ul>
![Page 25: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/25.jpg)
25
New result HTML List<ul> <li>Everyday Italian</li> <li>Harry Potter</li> <li>Learning XML</li> <li>XQuery Kick Start</li> </ul>
![Page 26: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/26.jpg)
26
Another FLWOR Expression<doubles>{ for $s in doc("students.xml")//student let $m := $s/major where count($m) ge 2 order by $s/@id return <double>
{ $s/name/text()} </double>}</doubles>
![Page 27: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/27.jpg)
27
The Difference between for and let
![Page 28: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/28.jpg)
28
The Difference between for and let
:=in
![Page 29: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/29.jpg)
29
The Difference between for and let
![Page 30: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/30.jpg)
30
The Difference between for and let
![Page 31: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/31.jpg)
31
FLWOR Basic Building Blocks
![Page 32: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/32.jpg)
32
General rules
• for and let may be used many times in any order
• only one where is allowed • many different sorting criteria can be
specified (descending, ascending, etc.)
![Page 33: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/33.jpg)
33
Reversing order• Reverses the order of a sequence, for
nodes or atomic values
• reverse (( 1, 2, 3))-> 321
![Page 34: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/34.jpg)
34
Joining documentsfor $p in doc("www.irs.gov/taxpayers.xml")//person for $n in doc("neighbors.xml")//neighbor[ssn = $p/ssn] return <person> <ssn> { $p/ssn } </ssn> { $n/name } <income> { $p/income } </income> </person>
![Page 35: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/35.jpg)
35
Two-way join in a where Clausefor $item in doc(“ord.xml”)//item, $product in doc(“cat.xml”)//productwhere $item/@num = $product/numberreturn <item num=“{$item/@num}” name=“{$product/name}” quan=“{$item/@quantity}” />
![Page 36: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/36.jpg)
36
Aggregating• Make summary calculations on grouped
data• Functions:
– sum, avg, max, min, count
![Page 37: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/37.jpg)
37
Conditionalsfor $b in doc(“bib.xml”)/book
return <short> {$b/title} <author> {if ( count($b/author) < 3 ) then $b/author else ( $b/author[1], <author>and others</author>) } </author> </short>
![Page 38: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/38.jpg)
38
Nesting Conditional Expressions• Conditional expressions can be nested• ‘else if’ functionality is provided
• if ( count($b/author) = 1 ) then $b/author else if (count($b/author) = 2 )then (: .. :) else ( $b/author[1], <author>and others</author>)
![Page 39: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/39.jpg)
39
Logical Expressions• and, or operators:
– and has precedence over or– Parentheses can change precedence
if ($isDiscounted and ($discount > 5 or $discount < 0 ) ) then 5 else $discount
• not function for negations: if (not($isDiscounted)) then 0 else $discount
![Page 40: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/40.jpg)
40
XQuery Built-in Functions XQuery function namespace URI is:
http://www.w3.org/2005/02/xpath-functionsdefault prefix: fn:.• E.g.: fn:string() or fn:concat(). • fn: is the default prefix of the namespace, the
function names does not need to be prefixed when called.
![Page 41: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/41.jpg)
41
Built-in Functions• String-related
– substring, contains, matches, concat, normalize-space, tokenize
• Date-related– current-date, month-from-date, adjust-time-to-
timezone• Number-related
– round, avg, sum, ceiling• Sequence-related
– index-of, insert-before, reverse, subsequence, distinct-values
![Page 42: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/42.jpg)
42
Built-in Functions (2)• Node-related
– data, empty, exists, id, idref• Name-related
– local-name, in-scope-prefixes, QName, resolve-QName
• Error handling and trapping– error, trace, exactly-one
• Document and URI-related– collection, doc, root, base-uri
![Page 43: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/43.jpg)
43
Function callsdoc("books.xml")//book[substring(title,1,5)='Harry']
let $name := (substring($booktitle,1,4))
<name>{upper-case($booktitle)}</name>
![Page 44: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/44.jpg)
44
for $x in doc("http://www.dcs.warwick.ac.uk/~acristea/courses/CS253/2009/books.xml")//book/title
for $y in data($x)for $name in (substring($y,1,4))
return $name
![Page 45: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/45.jpg)
45
User Defined Functionsdeclare function
prefix:function_name($parameter AS datatype) AS returnDatatype { (: ...function code here... :) };
![Page 46: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/46.jpg)
46
User-defined Functionsdeclare function depth($e AS xsd:integer) AS xsd:integer
{ if (empty($e/*) then 1 else max(for $c in $e/* return depth($c)) ) +1};
(: usage :) for $b in doc(“bib.xml”)/book
return depth($b)
![Page 47: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/47.jpg)
47
Existential and Universal Quantifiers• for $b in doc(“bib.xml”)/book
where some $author in $b/author satisfies $author/text() = “Ullman”return $b
• for $b in doc(“bib.xml”)/bookwhere every $author in $b/author satisfies $author/text() = “Ullman”return $b
Return books where all authors are “Ullman”
Return books where at least one author is “Ullman”
![Page 48: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/48.jpg)
48
Comments
![Page 49: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/49.jpg)
49
Comparisons• Value comparisons
Eq, ne, lt, le, gt, geUsed to compare individual valuesEach operand must be a single atomic value (or a
node containing a single atomic value)• General comparisons
=, !=, <, <=, >, >=Can be used with sequences of multiple items
![Page 50: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/50.jpg)
50
Example
![Page 51: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/51.jpg)
51
XQuery Syntax• Declarative, functional language
~ SQL• Nested expressions• Case sensitive• White spaces:
– Tabs, space, CR, LF– Ignored between language constructs– Significant in quoted strings
• No special EOL character
![Page 52: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/52.jpg)
52
Keywords and names• Keywords and operators
– Case-sensitive, generally lower case– May have several meanings depending on the
context• E.g. “*” or “in”
– No reserved words• All names must be valid XML names
– variables, functions, elements, attributes– Can be associated with a namespace
![Page 53: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/53.jpg)
53
XQuery gives you a choice:• Path Expressions:
– If you just want to copy certain elements and attributes as is
• FLWOR Expressions:– Allow sorting– Allow adding elements/attributes– Verbose, but can be clearer
![Page 54: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/54.jpg)
54
XQuery tools• XStylus Studio 2007
http://www.stylusstudio.com/xml_download.html (free trial version)– See also short XQuery intro at:
http://www.stylusstudio.com/xquery_primer.html
![Page 55: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/55.jpg)
55
Other info:
–XQuery on Distributed Resources–Extensions for generic programming with XML
![Page 56: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/56.jpg)
56
XQuery on Distributed Sources
![Page 57: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/57.jpg)
57
![Page 58: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/58.jpg)
58
![Page 59: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/59.jpg)
59
![Page 60: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/60.jpg)
60
![Page 61: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/61.jpg)
61
![Page 62: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/62.jpg)
62
XML and programming• XSLT, XPath and XQuery provide tools for
specialized tasks. • But many applications are not covered:
– domain-specific tools for concrete XML languages
– general tools that nobody has thought of yet
![Page 63: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/63.jpg)
63
XML in general-purpose programming languages
• parse XML documents into XML trees • navigate through XML trees • construct XML trees • output XML trees as XML documents • DOM and SAX are corresponding APIs that
are language independent and supported by numerous languages. JDOM is an API that is tailored to Java.
![Page 64: Topics in Database Systems: XQuery](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815a95550346895dc811b7/html5/thumbnails/64.jpg)
64
XQuery Conclusion• We have learned:
– XQuery definition– Usage scenarios– Comparison w. XSLT and XPath– Capabilities– Functions, path expressions and
predicates– FLWOR