Download presentation
Presentation is loading. Please wait.
Published byGodfrey Allen Modified over 9 years ago
1
Unit 04 : XQuery COMP 5323 Web Database Technologies and Applications 2014
2
This PowerPoint is prepared for educational purpose and is strictly used in the classroom lecturing. We have adopted the "Fair Use" doctrine in this PowerPoint which allows limited copying of copyrighted works for educational and research purposes. Doctrine of Fair Use
3
XQuery XQuery is case-sensitive XQuery elements, attributes, and variables must be valid XML names An XQuery string value can be in single or double quotes An XQuery variable is defined with a $ followed by a name, e.g. $bookstore XQuery comments are delimited by (: and :), e.g. (: XQuery Comment :)
4
XML Query Language Requirements Query Language Syntax – One query language syntax for people to read and write. – One query language syntax MUST be expressed in XML in a way that reflects the underlying structure of the query Declarativity – Declarative. There is no particular evaluation strategy. Xquery 3.0 - http://www.w3.org/TR/xquery-30/
5
XML Querying5 XQuery XQuery is a general purpose query language for XML data Various features of XQuery implemented in SQL Server XQuery is derived from the Quilt query language, which itself borrows from SQL, XQL and XML-QL XQuery 1.0 became a W3C Recommendation January 23, 2007 Xquery 3.0 currently proposed by the World Wide Web Consortium (W3C) XQuery uses a FL(O)WR expression for … let … where.. return … syntax for SQL from where SQL where return SQL select let allows temporary variables, and has no equivalent in SQL
6
XQuery and XPath
7
What’s XQuery XQuery is designed to meet the requirements identified by the W3C XML Query Working Group “XML Query 1.0 Requirements” and the use cases in “XML Query Use Cases”. XQuery is designed to be a small, easily implementable language. XQuery is flexible enough to query a broad spectrum of XML information sources, including both databases and documents. XQuery defines a human-readable syntax for that language
8
XQL XQL is a query language designed specifically for XML Not all constructs found in XPath were needed for queries, and some constructs used in XQL are not found in XPath, but the two languages share a common subset.
9
XQL Example red ………. XQL : vechicles/vechicle/color Result red
10
Bookstore.xml Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95
11
XQuery is built on XPath expressions doc("bookstore.xml") – The doc() function is used to open the "books.xml" file doc("bookstore.xml")/bookstore/book/title – XQuery uses path expressions to navigate through elements in an XML document doc("bookstore.xml")/bookstore/book[price<30] – XQuery uses predicates to limit the extracted data from XML documents Everyday Italian Harry Potter XQuery Kick Start Learning XML
12
Value Comparison A value comparison is used to compare single values only – First are operands evaluated – If both operands consist of exactly one value, and value types are appropriate, the comparison is true if values are the same The following value comparison is true fn:doc(“bookstore.xml”)//title/@lang eq “en” only if the XPath expression returns only one @lang node and the node has a value of “en”, otherwise XQuery will raise an error (more nodes), or return false (not “en” ) The following value comparison returns true //author[@sex="male"] eq James McGovern XPath 2.0 Quick Reference: http://www.xml.dvint.com/docs/XPath-2.pdf
13
Constructors XQuery provides constructors that can create XML structures within a query. Constructors are provided for element, attribute, document, text, comment, and processing instruction nodes. for $x in ( 1 2 ) return $x
14
Xquery Grammar FLWORExpr ::= (ForClause | LetClause)+ WhereClause? OrderByClause? "return" ExprSingle ForClause ::= VarName TypeDeclaration? PositionalVar? "in" ExprSingle ("," "$" VarName TypeDeclaration? PositionalVar? "in" ExprSingle)* LetClause ::= VarName TypeDeclaration? ":=" ExprSingle ("," "$" VarName TypeDeclaration? ":=" ExprSingle)* WhereClause ::= "where" Expr http://www.w3.org/2002/11/xquery-xpath-applets/xquery-bnf.html
15
FLWORExpr http://documentation.softwareag.com/webm ethods/tamino/ins951/xqueryref/ref- FLWORExpr.htm
16
FLOWR doc("bookstore.xml")/bookstore/book[price>30]/title or for $x in doc("bookstore.xml")/bookstore/book where $x/price>30 return $x/title XQuery Kick Start Learning XML for $x in doc("bookstore.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
17
customers.xml 408-516-4963 408-087-1234 408-111-222 408-516-4963 408-087-1234 408-111-222
18
WHERE WHERE Clause – Selects values that satisfy some condition expressed as a Boolean expression – Universal and Existential Quantification supported See Examples next two slides
19
Example 1 for $c in /customers/customer where some $p in $c/phone satisfies $p/@type = "cell" return $c 408-516-4963 408-087-1234 408-111-222 408-516-4963 408-087-1234 408-111-222
20
Example 2 for $c in /customers/customer where some $p in $c/phone satisfies $p/@type = “home" return $c 408-516-4963 408-087-1234 408-111-222
21
order by and return The result of applying for and where clauses is a sequence of tuples bound to variables The return clause is executed for each tuple and produces the result of a FLWOR expression Each execution of a return clause is an evaluation of expressions in curly braces of the element constructor Example: return {$c} If there is no order by clause before the return clause, the query result is sorted according to the way the for clause iterates through values The order by clause is used to sort query result
22
OrderBy for $x in doc("bookstore.xml")/bookstore/book/title order by $x return $x – The expression above will select all the title elements under the book elements that are under the bookstore element, and return the title elements in alphabetical order for $x in doc("bookstore.xml")/bookstore/book/title order by $x return {$x} See the next slide for the query result
23
Query Result Everyday Italian Harry Potter Learning XML XQuery Kick Start
24
Positional Variables The FOR clause supports positional variables which identify the position of a given item in the expression that generated it for $t at $i in doc("bookstore.xml")//title return {string($t)} Everyday Italian Harry Potter XQuery Kick Start Learning XML
25
students.xml James Bond Roger Smith Emmy Smith
26
Tree Representation student surname d1d1 students.xml e1e1 students e2e2 e5e5 student a1a1 t1t1 e3e3 e4e4 s1 sid James name t2t2 Bond surname t3t3 e6e6 e7e7 Roger name t4t4 Smith a2a2 s2 sid Emmy name Smith s3 sid a3a3 e8e8 e 10 t5t5 t6t6 e9e9
27
faculty.xml DB Sys s1 A+ s2 B
28
faculty.xml Issues in DB&Inf Sys s1 A+ s2 D DB Sys Pavle s2 D
29
Tree Representation s1 A+s2 student d 101 faculty.xml e 101 faculty a 101 e 102 e 110 e 118 name Science course a 102 t 101 e 103 e 104 e 107 e 105 a 103 p13 pid year 2007 DB Sys name e 106 t 102 sid grade t 103 e 108 e 109 t 104 sid grade t 105 B
30
OrderBy - Sorting {for $s in //students/student order by $s/name return {$s/name} } Emmy James Roger
31
Inverted Queries (Query nesting) The natural way of traversing an XML document is from the root towards leaves Queries asking for subordinated concepts are simple Inverted queries are implemented by query nesting The outer query returns a sequence of subordinated concepts, whereas the inner query returns a sequence of corresponding superior concepts Next query returns a sequence of course groups passed by each student from the XML document faculty.xml
32
Inverted Sequences { for $s in distinct-values(//faculty/course/student/sid) return { for $c in distinct-values (//faculty/course[student/sid=$s]/name) return {$c} } } s1 s2
33
Result DB Sys Issues in DB? Sys DB Sys Issues in DB? Sys
34
Analysis Of The Inverted Query The outer query binds the variable $s to those sid elements that have different string values using the function fn:distinct- values For each $s value, the inner query traverses the whole faculty.xml document looking for those course elements that have student elements with the sid value equal to the current value of the variable $s and binds name elements of these course elements to the variable $c The query constructs the result for each value of $s and the sequence of associated $c values
35
Queries with Aggregates Database queries frequently use aggregate functions with grouping The following query returns courses having the number of students enrolled grater than a given constant In the query, the variable $c binds in each course element, and the variable $s binds all student elements that are subordinated to the current course The value of the variable $s is a sequence of student elements
36
Aggregate Query { for $c in //faculty/course let $s:=$c/student for $t in $c[count($s) > 1] return {$t/name} {count($s)} }
37
Result DB Sys 2 Issues in DB? Sys 2
38
Conditional Expressions XQuery provides usual if, then, else clauses for program branching Each of these three clauses is associated with an expression The expression associated with the if clause is called test expression and it is placed within curly braces If the test expression is true, or it returns a non empty sequence, the then expression is executed, otherwise the else expression is executed
39
XQuery with a Conditional Expression for $c in fn:doc(faculty.xml)/faculty/course return <course name="{$c/name/text()}“ year="{$c/@year}"> { if ($c/lecturer) then $c/lecturer else "Lecturer is still undetermined!" }
40
Result <course name="DB Sys" year="2004">Lecturer is still undetermined! <course name="Issues in DB? Sys" year="2004">Lecturer is still undetermined! <course name="DB Sys" year="2003"> Pavle
41
Joining Documents XQuery allows joining of XML documents Let d 1 and d 2 be two XML documents, p 1 and p 2 paths through d 1 and d 2, respectively, and let $t 1 binds result returned by p 1, and $t 2 binds result returned by p 2 Joining of d 1 and d 2 (or parts of them) is accomplished by: – Defining a nested query with p 1 in the outer and p 2 in the inner query, and – Equating $t 1 and $t 2 in the inner query
42
Example Books that cost more at Amazon than MyBooks let $amazon := doc(“http://www.amazon.co.uk/books.xml”), $mb := doc(“http://www.mybooks.co.uk/books.xml”) for $a in $amazon/bib/book, $b in $mb/bib/book where $a/isbn = $b/isbn and $a/price > $b/price return { $a/title, $a/price, $b/price }
43
FOR vs LET for $x in (1 to 5) return {$x} Result 1 2 3 4 5 let $x := (1 to 5) return {$x} Result 1 2 3 4 5
44
A Running Example <!DOCTYPE BARS [ ]>
45
Query Sample Let us produce the PRICE elements (from all bars) for all the beers that are sold by Vinc ’ s Bar. The output will be BBP elements with the names of the bar and beer as attributes and the price element as a subelement. Create a triple for-loop, with variables ranging over all BEER elements, all BAR elements, and all PRICE elements within those BAR elements. Check that the beer is sold at Vinc ’ s Bar and that the name of the beer and theBeer in the PRICE element match. Construct the output element.
46
Query Sample let $bars := doc(”bars.xml”)/BARS for $beer in $bars/BEER for $bar in $bars/BAR for $price in $bar/PRICE where $beer/@soldAt = ”VincBar” and $price/@theBeer = $beer/@name return {$price}
47
Element Comparison /BARS/BAR[@name=”VincBar”]/ PRICE[@theBeer=”Bud”] eq /BARS/BAR[@name=”MaryBar”]/ PRICE[@theBeer=”Bud”] It returns false, even if Vinc and Mary charge the same for Bud.
48
Some The bars that sell at least one beer for less than $2. for $bar in doc(”bars.xml”)/BARS/BAR where some $p in $bar/PRICE satisfies $p < 2.00 return $bar/@name
49
Every The bars that sell no beer for more than $5. for $bar in doc(”bars.xml”)/BARS/BAR where every $p in $bar/PRICE satisfies $p <= 5.00 return $bar/@name
50
XQuery Functions XQuery includes over 100 built-in functions. – There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more. – The URI of the XQuery function namespace is http://www.w3.org/2005/02/xpath-functions – e.g. fn:abs(), fn:string(), fn:substring(string,start,len) Can also define your own functions in XQuery.
51
XQuery Functions declare function local:identityFunction($v as xs:integer) { math:sqrt($v) }; local:identityFunction(17) Result 4.123105625617661
52
Function Item One of the most distinguishing features added in XQuery 3.0 are function items, also known as lambdas or lambda functions. They make it possible to abstract over functions and thus write more modular code. let $f := function($x, $y) { $x + $y } return $f(17, 25)
53
Reference www.w3.org Priscilla Walmsley, XQuery: Search Across a Variety of XML Data, O Reilly Media, 2007
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.