Unit 04 : XQuery COMP 5323 Web Database Technologies and Applications 2014.

Slides:



Advertisements
Similar presentations
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XPath, NameSpaces.
Advertisements

XML Data Management 8. XQuery Werner Nutt. Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be.
Dr. Alexandra I. Cristea XPath and Namespaces.
XQuery John Annechino Steven Pow. Agenda What is XQuery? Uses of XQuery XQuery vs. XSLT Syntax –Built-In Functions –FLWOR –if-then-else –User-Defined.
XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
Friday, September 4 th, 2009 The Systems Group at ETH Zurich XML and Databases Exercise Session 6 courtesy of Ghislain Fourny/ETH © Department of Computer.
QSX (LN 3)1 Query Languages for XML XPath XQuery XSLT (not being covered today!) (Slides courtesy Wenfei Fan, Univ Edinburgh and Bell Labs)
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 357 Database Systems I Query Languages for XML.
1 COS 425: Database and Information Management Systems XML and information exchange.
XML and The Relational Data Model
XQuery – The W3C XML Query Language Jonathan Robie, Software AG Don Chamberlin, IBM Research Daniela Florescu, INRIA.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
1 XQuery Values FLWR Expressions Other Expressions.
1 Query Languages for XML XPath XQuery XSLT. 2 The XPath/XQuery Data Model uCorresponding to the fundamental “relation” of the relational model is: sequence.
Processing of structured documents Spring 2003, Part 8 Helena Ahonen-Myka.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
XMLII XSchema XSchema XQuery XQuery. XML Schema XML Schema is a more sophisticated schema language which addresses the drawbacks of DTDs. Supports XML.
XML Document Type Definitions XML Schema. Motivation for Semistructured data Serves as a model suitable for integration of databases Notations such as.
Lecture 7 of Advanced Databases XML Querying & Transformation Instructor: Mr.Ahmed Al Astal.
Lecture 21 XML querying. 2 XSL (eXtensible Stylesheet Language) In HTML, default styling is built into browsers as tag set for HTML is predefined and.
Lecture 6 of Advanced Databases XML Schema, Querying & Transformation Instructor: Mr.Ahmed Al Astal.
COP4540 Database Management System Final Review Reviewed by Ramakrishna. Parts of this are taken from Fernando Farfan’s presentation.
SDPL 20067: Querying XML with XQuery1 7 Querying XML n How to access data sources as XML? n XQuery, W3C XML Query Language –Candidate Rec, November 2005.
Introduction to XML and XQuery Guangjun (Kevin) Xie.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
Lecture 6 of Advanced Databases XML Querying & Transformation Instructor: Mr.Eyad Almassri.
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.
1 Querying XML Documents. 2 Objectives How XML generalizes relational databases The XQuery language How XML may be supported in databases.
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
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.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation eXist Update Lecturer.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
Lecture 22 XML querying. 2 Example 31.5 – XQuery FLWOR Expressions ‘=’ operator is a general comparison operator. XQuery also defines value comparison.
Introduction to XQuery Bun Yue Professor, CS/CIS UHCL.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Query Data Model Lecturer.
1 XQuery uXQuery extends XPath to a query language that has power similar to SQL. uUses the same sequence-of-items data model. uXQuery is an expression.
August Chapter 6 - XPath & XPointer Learning XML by Erik T. Ray Slides were developed by Jack Davis College of Information Science and Technology.
Database Systems Part VII: XML Querying Software School of Hunan University
SDPL 2002Notes 9: XQuery1 9 Querying XML Data and Documents n XQuery, W3C XML Query Language –"work in progress", Working Draft, 30 April 2002 –joint work.
Namespace information are represented as namespace node which maps in scope on an element Attach to every element node where namespace is declared root.
[ Part III of The XML seminar ] Presenter: Xiaogeng Zhao A Introduction of XQL.
XML query. introduction An XML document can represent almost anything, and users of an XML query language expect it to perform useful queries on whatever.
SDPL 20077: Querying XML with XQuery1 7 Querying XML n How to access XML data sources? n XQuery, XML Query Lang, W3C Rec, 01/ '07 –joint work by XML Query.
1 Typing XQuery WANG Zhen (Selina) Something about the Internship Group Name: PROTHEO, Inria, France Research: Rewriting and strategies, Constraints,
XML e X tensible M arkup L anguage (XML) By: Albert Beng Kiat Tan Ayzer Mungan Edwin Hendriadi.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
What is XLink? XLink is short for XML Linking Language XLink is used to create hyperlinks in XML documents Any element in an XML document can behave as.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
This PowerPoint is based on slides from: Rajshekhar Sunderraman, Department of Computer Science Georgia State University.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
XQUERY The contents of this slide are obtained from various sources including, Wikipedia, W3School, Stanford website etc. January 2011 Dr.Kwanchai Eurviriyanukul.
SDPL 2005Notes 7: XQuery1 7 Querying XML n How to access different sources (DBs, docs) as XML? n XQuery, W3C XML Query Language –"work in progress", (last.
XML. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data.
XML Query languages--XPath. Objectives Understand XPath, and be able to use XPath expressions to find fragments of an XML document Understand tree patterns,
Query Languages for XML
Querying and Transforming XML Data
Session IV Chapter 16 – XQuery
8 Querying XML How to access different sources (DBs, docs) as XML?
CS 480: Database Systems Lecture 28 March 22, 2013.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
SQL: Structured Query Language DML- Queries Lecturer: Dr Pavle Mogin
XQuery Leonidas Fegaras.
Session II Chapter 16 – XQuery
Query Languages for XML
Presentation transcript:

Unit 04 : XQuery COMP 5323 Web Database Technologies and Applications 2014

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

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 :)

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

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

XQuery and XPath

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

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.

XQL Example red ………. XQL : vechicles/vechicle/color Result red

Bookstore.xml Everyday Italian Giada De Laurentiis Harry Potter J K. Rowling XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan Learning XML Erik T. Ray

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

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 eq “en” only if the XPath expression returns only 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 eq James McGovern XPath 2.0 Quick Reference:

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

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

FLWORExpr ethods/tamino/ins951/xqueryref/ref- FLWORExpr.htm

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

customers.xml

WHERE WHERE Clause – Selects values that satisfy some condition expressed as a Boolean expression – Universal and Existential Quantification supported See Examples next two slides

Example 1 for $c in /customers/customer where some $p in $c/phone satisfies = "cell" return $c

Example 2 for $c in /customers/customer where some $p in $c/phone satisfies = “home" return $c

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

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

Query Result Everyday Italian Harry Potter Learning XML XQuery Kick Start

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

students.xml James Bond Roger Smith Emmy Smith

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

faculty.xml DB Sys s1 A+ s2 B

faculty.xml Issues in DB&Inf Sys s1 A+ s2 D DB Sys Pavle s2 D

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

OrderBy - Sorting {for $s in //students/student order by $s/name return {$s/name} } Emmy James Roger

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

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

Result DB Sys Issues in DB? Sys DB Sys Issues in DB? Sys

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

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

Aggregate Query { for $c in //faculty/course let $s:=$c/student for $t in $c[count($s) > 1] return {$t/name} {count($s)} }

Result DB Sys 2 Issues in DB? Sys 2

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

XQuery with a Conditional Expression for $c in fn:doc(faculty.xml)/faculty/course return <course name="{$c/name/text()}“ { if ($c/lecturer) then $c/lecturer else "Lecturer is still undetermined!" }

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

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

Example Books that cost more at Amazon than MyBooks let $amazon := doc(“ $mb := doc(“ 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 }

FOR vs LET for $x in (1 to 5) return {$x} Result let $x := (1 to 5) return {$x} Result

A Running Example <!DOCTYPE BARS [ ]>

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.

Query Sample let $bars := doc(”bars.xml”)/BARS for $beer in $bars/BEER for $bar in $bars/BAR for $price in $bar/PRICE where = ”VincBar” and = return {$price}

Element Comparison eq It returns false, even if Vinc and Mary charge the same for Bud.

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

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

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 – e.g. fn:abs(), fn:string(), fn:substring(string,start,len) Can also define your own functions in XQuery.

XQuery Functions declare function local:identityFunction($v as xs:integer) { math:sqrt($v) }; local:identityFunction(17) Result

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)

Reference Priscilla Walmsley, XQuery: Search Across a Variety of XML Data, O Reilly Media, 2007