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 1 Lecturer : Dr. Pavle Mogin

2 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 1 Plan For XML Query Languages Why XQuery? What is XQuery XQuery expressions –Arithmetic expressions, –Comparison expressions, –Logical expressions –Element constructor –FLWOR expressions –Background: Query Data Model, XPath –Redings: 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 Why XQuery? A number of different XML query languages like: –XPath, –Lorel, –XML-QL, and –Quilt have been developed during the last two decades A number of years ago, W3C proposed XQuery XQuery has been adopted as a native XML database query language In parallel, SQL standard committee has defined SQL/XML to extend SQL:1999

4 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 3 What kind of a Query Language is XQuery XQuery is a functional language –It is composed of expressions –Expressions return results without any side effects XQuery expressions are produced by combining simpler expressions using operators and key words Key words are written using lower case letters Besides primary expressions you saw in Query Data Model (literals, variables, function calls), XQuery supports: –XPath expressions, –Arithmetic expressions, –Element constructor, –FLWOR expressions, and –Conditional expressions

5 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 4 Arithmetic Expressions XQuery supports: –Common arithmetic operators +, -, *, div, mod, and –Aggregate functions like count(), sum(), avg(), min(), max() –Operator div is used instead of “ / ” Arithmetic operators are defined on numeric values Aggregate functions are defined on sequences of (numeric) values –If an operand is a node, the arithmetic operator is applied on its typed value –If an operand is an empty sequence, the operator also returns an empty sequence

6 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 5 Comparison Expressions Comparison expressions allow two values to be compared XQuery provides three kinds of comparison expressions –Value comparison with operators eq, ne, lt, le, gt, ge –General comparison with operators =, !=,, >= –Node comparison with operators is, > Operands of comparison expressions are called range expressions Formally, o1θo2o1θo2 where o 1, o 2 are operands and θ is a comparison operator

7 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 6 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 o 1 θo 2 evaluates true The following value comparison is true fn:doc(“students.xml”)//student[@sid eq “s1”] only if the XPath expression returns only one @sid node and the node has a value of “s1”, otherwise XQuery will raise an error (more nodes), or return false (not “s1” ) The following value comparison returns true 5 eq 5

8 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 7 General Comparison A general comparison is used to compare sequences of any length General comparisons are existentially quantified, they return true if at lest one pair of elements from both sequences satisfy the comparison operator The following general comparison evaluates true fn:doc(“students.xml”)//student[@sid = “s1”] if the XPath expression returns at least one @sid node with a value of “s1”, otherwise XQuery will return false The following general comparison evaluates true (2, 3) = (3, 4, 5)

9 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 8 Node Comparison Node comparisons are used to compare two nodes, by their identity, or by their document order Each operand has to be either a single node, or an empty sequence –If an operand is an empty sequence, the result is also an empty sequence –A comparison with the is operator returns true, if both operands have the same identifier –A comparison with << operator returns true, if the left operand has a smaller identifier value than the right operand with regard to the document order

10 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 9 Logical Expressions A logical expression is: –an and-expression, –an or-expression, or –A not-expression If a logical expression does not raise an error, its value is always either true or false The following expression returns true 1 eq 1 and 2 le 3 XQuery provides a function named fn:not –The function reduces its parameter to a Boolean value –It returns true if the Boolean value of the parameter is false and vice versa –fn:not(2 < 1) = T

11 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 10 Comments Comments are in XQuery placed between “ (: ” and “ :) ” Example: (:SWEN432 people enjoy doing XQuery:)

12 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 11 Element Constructors XPath expressions select existing nodes XQuery queries should return new XML documents or XML fragments Element constructor expressions are used to construct new XML There are: –Direct element constructors, where element names are constants, and values can be either constants, or results of evaluating expressions, and –Computed element constructors, where even element names can be evaluated We consider the direct element constructors, only

13 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 12 Direct Element Constructor A direct element constructor contains: –Start and end tags, with a content between them –The content of an element constructor may be: Text, Other element constructors, and Expressions, placed between curly braces –Expressions within curly braces are evaluated and replaced by values –The result of evaluating an expression can be any sequence of nodes and atomic values –If an expression returns a node, the node and all its descendants will be included in the content of the element constructor

14 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 13 surname Query Data Model Tree of students.xml 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 surname Emmy name Smith s3 sid a3a3 e8e8 e 10 t5t5 t6t6 e9e9

15 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 14 Element Constructor Example The following is a constant only element constructor James Bond Let the variable $s be defined as for $s in fn:doc("students.xml") /students/student  @sid = "s1"  Then the following element constructor returns the above constant only element constructor  $s/name, $s/surname 

16 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 15 FLWOR Expressions (1) FLWOR stands for –for, (used to iterate through the result of an XPath expression and to bind a variable in tern to each object of the result) –let, (used to bind a variable to the whole sequence of objects returned by an XPath expression) –where, (used to select those objects from the result returned by an XPath expression that satisfy given conditions) –order by, (used to sort the result of an XPath expression) and –return (used to construct the query result) FLWOR is pronounced as flower FLWOR expressions allow: –Iterating through XML documents –Binding variables to the iteration results, –Restructuring XML documents, and –Joining XML documents

17 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 16 FLWOR Expressions (2) A FLWOR expression has: –At lest one for or let clause, –At most one where clause, –At most one order by clause, and –Exactly one return expression

18 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 17 Return Clause and Element Constructor A return clause contains exactly one element constructor that may contain other element constructors Example 1: for $n in (1, 2) return (:Wrong! These are 2 element constructors:) Example 2: for $n in (1, 2) return (:Correct:)

19 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 18 for and let Clauses for and let clauses are used to bind variables to values returned by an expression for clause is also used to iterate over a sequence of values A FLWOR expression may contain more than one for and let clause, each containing at least one variable for $n in (2, 3), $m in (5, 6) return {$n} times {$m} is {$n*$m} The query returns 2 times 5 is 10 … 3 times 6 is 18

20 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 19 Difference Between for and let The for clause binds a variable to each value of a sequence, one at a time The let clause binds a variable to a whole sequence at once for $n in (1 to 3) let $m:=(1 to $n) return {$n} {$m} The query result will be: 1 1 2 1 2 3 1 2 3

21 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 20 More About for and let Assume a query contains just one variable The return clause executes the element constructor for each binding of the variable to values for $s in fn:doc(“students.xml”)/students/student return {$s/name} Result: James … Emmy There were 3 executions of the element constructor

22 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 21 More About for and let let $s:=fn:doc(“students.xml”)/students/student return {$s/name} Result: James Roger Emmy There was only 1 execution of the element constructor

23 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 22 Summary (1) XQuery is a query language proposed by W3C for XML documents and native XML databases XQuery is a functional language –It is composed of expressions –Expressions return results without any side effects XQuery supports various types of expressions: –XPath expressions, –Arithmetic expressions, –Comparison expressions, –Logical expressions The element constructor is the most important concept that makes the XQuery superior to XPath The element constructor is used to construct the result of a query

24 SWEN 432 Advanced Database Design and Implementation 2015 XQuery Language 23 Summary (2) FLOWR expression –for –let –where –order by –return for and let closes –The for clause binds a variable to each value of a sequence, one at a time –The let clause binds a variable to a whole sequence at once


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