XQuery Leonidas Fegaras.

Slides:



Advertisements
Similar presentations
XML Data Management 8. XQuery Werner Nutt. Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be.
Advertisements

XML Examples. Bank Information Basic structure: A-101 Downtown 500 … Johnson Alma Surrey … A-101 Johnson …
Web Data Management XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections.
XML May 3 rd, XQuery Based on Quilt (which is based on XML-QL) Check out the W3C web site for the latest. XML Query data model –Ordered !
XML, XML Schema, Xpath and XQuery Slides collated from various sources, many from Dan Suciu at Univ. of Washington.
CSE 6331 © Leonidas Fegaras XML and Relational Databases 1 XML and Relational Databases Leonidas Fegaras.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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?
Database Management Systems, R. Ramakrishnan1 Introduction to Semistructured Data and XML Chapter 27, Part D Based on slides by Dan Suciu University of.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
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)
Query Languages Aswin Yedlapalli. XML Query data model Document is viewed as a labeled tree with nodes Successors of node may be : - an ordered sequence.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
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.
Query Languages - XQuery Slides partially from Dan Suciu.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
XQuery – The W3C XML Query Language Jonathan Robie, Software AG Don Chamberlin, IBM Research Daniela Florescu, INRIA.
XML QUERY LANGUAGE Prepared by Prof. Zaniolo, Hung-chih Yang, Ling-Jyh Chen Modified by Fernando Farfán.
1 XQuery Values FLWR Expressions Other Expressions.
Xpath to XQuery February 23rd, Other Stuff HW 3 is out. Instructions for Phase 3 are out. Today: finish Xpath, start and finish Xquery. From Wednesday:
Querying XML February 12 th, Querying XML Data XPath = simple navigation through the tree XQuery = the SQL of XML XSLT = recursive traversal –will.
Processing of structured documents Spring 2003, Part 8 Helena Ahonen-Myka.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
Advisor: Prof. Zaniolo Hung-chih Yang Ling-Jyh Chen XML Query Language.
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.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 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.
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.
A Quilt, not a Camel Don Chamberlin Jonathan Robie Daniela Florescu May 19, 2000.
Lecture 22 XML querying. 2 Example 31.5 – XQuery FLWOR Expressions ‘=’ operator is a general comparison operator. XQuery also defines value comparison.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Nikos dimitrakas – IS4/2i1242/2i4042 spring XML Query Languages Database Systems (4th edition) Chapter 30.5, , Articles & Excerpts.
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.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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,
1 XML Data Management XPath Principles Werner Nutt.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
19 th International Unicode Conference San Jose, CA September W3C XML Query Paul Cotton, Microsoft 19 th Unicode Conference Sept 12, 2001.
CSE3201/CSE4500 XPath. 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
CC L A W EB DE D ATOS P RIMAVERA 2015 Lecture 7: SPARQL (1.0) Aidan Hogan
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
XQuery 1. In this lecture Summary of XQuery FLWOR expressions – For, Let, Where, Order by, Return FOR and LET expressions Collections and sorting 2.
Lecture 17: XPath and XQuery Wednesday, Nov. 7, 2001.
1 CSE544: Lecture 7 XQuery, Relational Algebra Monday, 4/22/02.
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.
Query Languages for XML
Object-Oriented Databases and the ODMG Standard
CC La Web de Datos Primavera 2017 Lecture 7: SPARQL [i]
Querying and Transforming XML Data
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
8 Querying XML How to access different sources (DBs, docs) as XML?
CS 480: Database Systems Lecture 28 March 22, 2013.
Querying XML and Semistructured Data
CS 405G: Introduction to Database Systems
CC La Web de Datos Primavera 2016 Lecture 7: SPARQL (1.0)
Lecture 12: XML, XPath, XQuery
Streams, Delayed Evaluation and a Normal Order Interpreter
CS 405G: Introduction to Database Systems
Presentation transcript:

XQuery Leonidas Fegaras

XQuery Influenced by ODMG OQL Evolved from Quilt Based on XPath Purely functional language may access elements from documents, may construct new values (elements), but cannot modify data any expression is a query query nesting is allowed at any place and on any level Strongly and statically typed both type checking and type inference Has formal semantics based on the XML abstract data model item: value or ordered tree ordered sequence of items literal: int, real, double, string

The Data Model Literals A sequence A node eg, “a string”, 10, 3.5 is an ordered list of items (nodes or atomic values) can contain heterogeneous values eg, (“a”,1,<a>“b”</a>) empty sequence: () there is no such thing as a nested sequence eg, ((),(1,(“a”,“b”)),“c”) is equivalent to (1,“a”,“b”,“c”) a value is also a singleton sequence A node may be element, text, attribute, documents, etc has identity follows a document order

Expressions Comma is sequence concatenation: eg, 1,2,3 which is equivalent to (1,2,3) Element construction: <tag> ... </tag> eg, <person><name>John Smith</name><phone>x1234</phone></person> may include attribute bindings in the start tag eg, <person ssn=“123456”>...</person> the content between the start and end tags (as well as the attribute values) is in construction mode to switch to computation mode, must use {} eg, <a x=“q” y=“{ 1+2 }”>{ 2+3 }=4+1</a> is equivalent to <a x=“q” y=“3”>5=4+1</a> Alternative construction: element { tagname } { content } attribute { attribute-name } { value } inside an element construction where tagname and attribute-name are expressions that return strings

XPath Expressions Starts from a root: document(“URL”) document(“bib.xml”)//book/[author/lastname=“Smith”]/title document(“book.xml”)/chapter[10]//figure[caption=“XML”] Extended with ID dereference: @idrefname-> document(“movies.xml”)//movie[title=“Matrix”]/@cast->name An XPath predicate acts as a filter: e[p] for each element in the sequence e, if p is true, then propagate the element to the output, otherwise discard it Existential semantics of predicates [A/B < 10] is true if at least one element returned by A/B is numeric and less than 10 note that [A/B < 10] is false if A/B returns the empty sequence The predicate may be a simple XPath [A/B] is true if A/B returns a non-empty sequence

Expressions Arithmetic operators: + - * div mod cast values to double, if possible (otherwise, is an error) a () operand results to a () if the two operands are sequences of n/m elements, then the result is a sequence of n*m elements! (1,2,3) + (80,90) = (81,91,82,92,83,93) Comparisons: = < > <= >= != Boolean operators: and, or, not(...) (lazy evaluation) Set operators: union, intersect, except Full-text search: contains contains(//book/title ,“XML”) if-then-else Aggregation: count, sum, avg, min, max avg(//book[title=“XML”]/price)

FLWR Expressions Similar to select-from-where queries in OQL for $b in document(“bib.xml”)//book where $b/author/name = “John Smith” and $b/year > 2000 return $b/title Syntax: ([ ] means optional) for $v in e [ where e ] [ order by ... ] return e let $v := e [ where e ] [ order by ... ] return e Order-by clause order by e [ ascending | descending ], ... May include sequences of for/let bindings let $x:=1 let $y:=2 return $x+$y Existential/universal quantification some $v in e satisfies e every $v in e satisfies e

Semantics of FLWR Expressions for $x in e [where pred] return body both pred and body may depend on the value of $x if the expression e returns the sequence of values (v1,v2,...,vn), then variable $x is bound to v1 first; if pred is true, then evaluate the body variable $x is bound to v2 next; if pred is true, then evaluate the body, etc ...; finally, variable $x is bound to vn; if pred is true, then evaluate the body all the resulting sequences from evaluating the body are concatenated eg, the query: for $a in (1,2,3,4) return $a+10 returns: (11,12,13,14) let $x:=e return body if the expression e returns the sequence of values (v1,v2,...,vn), then $x is bound to the entire sequence eg, the query: let $a :=(1,2,3,4) return $a,$a returns: (1,2,3,4,1,2,3,4)

Example May return: <books>{ for $b in document(‘books.xml’)//book where $b/author/firstname = ‘John’ and $b/author/lastname = ‘Smith’ return <book>{ $b/title, $b/price }</book> }</books> May return: <books> <book><title>XML</title><price>29.99</price></book> <book><title>DOM and SAX</title><price>40</price></book> </books>

What about this? Will return: <books>{ for $b in document(‘books.xml’)//book where $b/author/firstname = ‘John’ and $b/author/lastname = ‘Smith’ return <book> $b/title, $b/price </book> }</books> Will return: <books> <book>$b/title,$b/price</book> </books>

Equivalent Query <books>{ for $b in document(‘books.xml’)//book [author/firstname = ‘John’ and author/lastname = ‘Smith’] return <book>{ $b/title, $b/price }</book> }</books>

What about this? It is actually more accurate for multiple authors: <books>{ for $b in document(‘books.xml’)//book where $b/author/[firstname = ‘John’ and lastname = ‘Smith’] return <book>{ $b/title, $b/price }</book> }</books> It is actually more accurate for multiple authors: <book> <author><firtstname>Mary</firstname> <lastname>Smith</lastname> </author> <author><firtstname>John</firstname> <lastname>Travolta</lastname> </book>

Join May return: <bids>{ for $i in document(‘items.xml’)//item let $b:=document(‘bids.xml’)//bid[@item=$i/@id] order by $i/@id ascending return <bid item=‘{$i/@id}’>{ $i/name, <price>{max($b/price)}</price> }</bid> }</bids> May return: <bids> <bid item=‘3’><name>bicycle</name><price>100</price></bid> <bid item=‘5’><name>car</name><price>10000</price></bid> </bids>

Join 2 <bids>{ for $i in document(‘items.xml’)//item for $b in document(‘bids.xml’)//bid[@item=$i/@id] order by $i/@id ascending return <bid item=‘{$i/@id}’>{ $i/name, $b/price }</bid> }</bids>

Dependent Join <best_students>{ for $d in document(‘depts.xml’)//department[name=‘cse’] for $s in $d//gradstudent where $s/gpa > 3.5 return <student>{ $s/name, $s/gpa, count($d//gradstudent) }</student> }</best_students>

Using 'let' <best_students>{ let $d := document(‘depts.xml’)//department[name=‘cse’] for $s in $d//gradstudent where $s/gpa > 3.5 return <student>{ $s/name,$s/gpa, count($d//gradstudent) }</student> }</best_students>

What about this? It will return only one student: <best_students>{ let $d := document(‘depts.xml’)//department[name=‘cse’] let $s := $d//gradstudent[gpa > 3.5] return <student>{ $s/name,$s/gpa, count($d//gradstudent) }</student> }</best_students> It will return only one student: <best_students> <student> <name>John Smith</name><name>Mary Jones</name>... <gpa>3.6</gpa><gpa>4.0</gpa> </student> </best_students>

Existential Quantification <result>{ for $i in document(‘items.xml’)//item where some $b in document(‘bids.xml’)//bid[@item=$i/@id] satisfies $b/price > 1000 return <bid>{$i}</bid> }</result> which is equivalent to: where document(‘bids.xml’)//bid[@item=$i/@id] [price > 1000]

Universal Quantification <result>{ for $i in document(‘items.xml’)//item where every $b in document(‘bids.xml’)//bid[@item=$i/@id] satisfies $b/price > 1000 return <bid>{$i}</bid> }</result> which is equivalent to: where not(document(‘bids.xml’)//bid[@item=$i/@id] [price <= 1000])

Nested XQueries Group book titles by author: <result>{ for $a in distinct-nodes(document(‘bib.xml’)/bib /book[publisher=‘Wesley’]/author) return <author>{ $a, for $t in document(‘bib.xml’)/bib /book[author=$a]/title return $t }</author> }</result> To groupy-by as in relational DBs, distinct-nodes is typically needed to remove duplicate groups

More Nested XQueries <prices>{ for $a in document(‘www.amazon.com’)/book return <book> { $a/title, $a/price } { for $b in document(‘www.bn.com’)/book where $b/@isbn=$a/@isbn and $b/price < $a/price return $b/price } </book> }</prices>

Functions A function may be recursive define function best ( $x ) { max(document(‘bids.xml’)//bid[@item=$x]/price) } define function get_best ( $x ) { for $i in document(‘item.xml’)//item where $i/name = $x return <item>{ $i, best($i/@id) }</item> get_best(‘bicycle’) A function may be recursive eg, compute the total cost of a part that contains subparts