Presentation is loading. Please wait.

Presentation is loading. Please wait.

VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery.

Similar presentations


Presentation on theme: "VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery."— Presentation transcript:

1 VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery Language 2 Lecturer : Dr. Pavle Mogin

2 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 1 Plan For XML Query Languages FLWOR expressions continued –where clause, –Inverted queries, –Joins –order by, Conditional expressions –Readings: Ramakrisnan, Gehrke: Database Management Systems, Chapter 27, Section 7 XQuery 1.0: An XML Query Language, W3C Working Draft, 29 October 2004, http://www.w3.org/TR/2004/WD-xquery-20041029/ XQuery 1.0 and Xpath 2.0 Functions and Operators, W3C http://www.w3.org/TR/xpath-functions/

3 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 2 student Partial Tree of faculty.xml 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 s1 grade t 103 A+ e 108 e 109 t 104 sid s2 grade t 105 B

4 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 3 student course e 110 Subtree e 110 course a 104 t 106 e 111 e 112 e 115 e 113 a 105 p14 pid year 2007 Issues in DB&Inf Sys name e 114 t 107 sid s1 grade t 108 A+ e 116 e 117 t 109 sid s2 grade t 110 D

5 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 4 lecturer student course e 118 Subtree e 118 course a 106 t 111 e 119 e 120 e 121 a 107 p13 pid year 2003 DB Sys name t 112 Pavle e 122 e 123 t 113 sid s2 grade t 114 D

6 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 5 where Clause Selection conditions are expressed using where clause let $c:=fn:doc(“faculty.xml”)/faculty/course where $c[name = “Database Systems”] and $c[@year = “2007”] return {$c/student/sid} Output: s1 s2

7 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 6 were Clause and the Path Expressions Instead of placing a selection expression into the where clause, it may be embedded as a predicate into an XPath expression with the abbreviated syntax let $c:=fn:doc(“faculty.xml”)/faculty/course [name = “Database Systems”][@year = “2007”] return {$c/student/sid} Now, the variable $c is bound to all course elements that match the path fn:doc(“faculty.xml”)/faculty/course [name = “Database Systems”][@year = “2007”] The return clause constructs a element for each sid returned by $c/student/sid

8 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 7 Tuple Stream The result of applying for (and where ) clauses is a tuple stream A tuple is formed of values returned by a for clause –The number of variables in the for clause determines the number of components a tuple has Example: for $i in (1, 2), $j in (3, 4) returns the following tuple stream: (1, 3) (1, 4) (2, 3) (2, 4)

9 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 8 order by and return Clauses The return clause is executed for each tuple of a tuple stream 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 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

10 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 9 Query Nesting Query nesting is achieved by placing a FLOWER expression in curly braces within the start and end tag of an element constructor Example: for $n in (1, 2) return { for $m in ($n + 1) return } (: Answer: :)

11 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 10 Sorting the students.xml Document {for $s in fn:doc(“students.xml”)//students/student order by $s/name return {$s/name} } (: is the start tag of an element constructor:) Emmy James Roger

12 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 11 Inverted Queries 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 Example: –Next query returns a sequence of courses passed by each student (according to faculty.xml )

13 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 12 Inverted Sequences { for $s in distinct- values(fn:doc(“faculty.xml”)/faculty/course/student/sid ) return { for $c in distinct-values (fn:doc(“faculty.xml”)/faculty/course[student/ sid eq $s]/name) return {$c} } }

14 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 13 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 children with such a sid child that has a 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

15 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 14 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 tern each course element, and the variable $s binds all student elements that are subordinated to the current course element The value of the variable $s is a sequence of student elements

16 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 15 Aggregate Query { for $c in fn:doc(“faculty.xml”)/faculty/course let $s:=$c/student for $t in $c[count($s) gt 1] return {$t/name} {count($s)} }

17 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 16 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 The following query joins faculty.xml and students.xml and returns courses followed by the students enrolled

18 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 17 Join Query for $c in fn:doc(“faculty.xml”)/faculty/course return { for $s in fn:doc(“students.xml”)/students/ student[@sid = $c/student/sid] return { $s/name, $s/surname, $c/student[sid = $s/@sid]/grade } }

19 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 18 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 If the test expression is true, or it returns a non empty sequence, then the then expression is executed, otherwise the else expression is executed

20 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 19 XQuery with a Conditional Expression (1) for $c in fn:doc(“/db/pmogin/Faculty.xml”)/faculty/course return <course name="{$c/name/text()}“ year="{$c/@year}"> { if ($c/lecturer) then $c/lecturer else "Lecturer is still undetermined!" } The if then else block is included in an element constructor, hence curly braces needed

21 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 20 XQuery with a Conditional Expression (2) for $c in fn:doc("/db/pmogin/Faculty.xml")/faculty/course return if ($c/lecturer) then ( $c/lecturer ) else ( "Lecturer is still undetermined!" ) The if then else block contains element constructors, hence curly braces (embracing the if then else block) are not needed

22 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 21 Summary XQuery is a query language proposed by W3C for XML documents and native XML databases The basic form of an XQuery is a FLWOR expression Selection conditions can be included either in the where clause, or in XPath predicates Inverted queries transform an XML document in such a way that subordinated elements contain their superior elements XQuery supports joining different documents or brunches of a single document XQuery has both declarative and procedural clauses


Download ppt "VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation An Introduction to XQuery."

Similar presentations


Ads by Google