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

Slides:



Advertisements
Similar presentations
Advanced XSLT. Branching in XSLT XSLT is functional programming –The program evaluates a function –The function transforms one structure into another.
Advertisements

Querying on the Web: XQuery, RDQL, SparQL Semantic Web - Spring 2006 Computer Engineering Department Sharif University of Technology.
XML Examples. Bank Information Basic structure: A-101 Downtown 500 … Johnson Alma Surrey … A-101 Johnson …
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
XML: Extensible Markup Language
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Structured Query Language (SQL)
Paper by: A. Balmin, T. Eliaz, J. Hornibrook, L. Lim, G. M. Lohman, D. Simmen, M. Wang, C. Zhang Slides and Presentation By: Justin Weaver.
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.
Introduction to Structured Query Language (SQL)
A Graphical Environment to Query XML Data with XQuery
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
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.
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.
Database Systems More SQL Database Design -- More SQL1.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
Processing of structured documents Spring 2003, Part 8 Helena Ahonen-Myka.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
XML files (with LINQ). Introduction to LINQ ( Language Integrated Query ) C#’s new LINQ capabilities allow you to write query expressions that retrieve.
SPARQL All slides are adapted from the W3C Recommendation SPARQL Query Language for RDF Web link:
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Chapter 3 Single-Table Queries
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.
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.
1 XML Data Management 7. XLST Werner Nutt. Kernow and Saxon To demo XSLT, we use Kernow –Kernow is a (graphical) front end for Saxon Saxon is an XSLT,
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall Querying on the Web: XQuery, RDQL, SparQL Semantic Web.
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.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Exam and Lecture Overview.
Database Systems Part VII: XML Querying Software School of Hunan University
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents.
1 XML Data Management XPath Principles Werner Nutt.
XML Data Management XPath Exercises: Right or Wrong? Werner Nutt.
XML Query: xQuery Reference: Xquery By Priscilla Walmsley, Published by O’Reilly.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
1 XML Data Management XLST Werner Nutt. A Hello World! Stylesheet world.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Grouping Robin Burke ECT 360. Outline Extra credit Numbering, revisited Grouping: Sibling difference method Uniquifying in XPath Grouping: Muenchian method.
CSE 6331 © Leonidas Fegaras XQuery 1 XQuery Leonidas Fegaras.
Querying XML, Part II Zachary G. Ives University of Pennsylvania CIS 455 / 555 – Internet and Web Systems February 5, 2008.
XML Data Management XPath Exercises: Right or Wrong? Werner Nutt.
1 The XPath Language. 2 XPath Expressions Flexible notation for navigating around trees A basic technology that is widely used uniqueness and scope in.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
XML: Extensible Markup Language
More SQL: Complex Queries,
Querying and Transforming XML Data
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
SQL Structured Query Language 11/9/2018 Introduction to Databases.
CS 405G: Introduction to Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
XQuery Leonidas Fegaras.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

XML Data Management 8. XQuery Werner Nutt

Requirements for an XML Query Language David Maier, W3C XML Query Requirements: Closedness: output must be XML Composability: wherever a set of XML elements is required, a subquery is allowed as well Support for key operations: –selection –extraction, projection –restructuring –combination, join –fusion of elements

Requirements for an XML Query Language Can benefit from a schema, but should also be applicable without Retains the order of nodes Formal semantics: –structure of results should be derivable from query –defines equivalence of queries Queries should be representable in XML documents can have embedded queries

How Does One Design a Query Language? In most query languages, there are two aspects to a query: –Retrieving data (e.g., from … where … in SQL) –Creating output (e.g., select … in SQL) Retrieval consists of –Pattern matching (e.g., from … ) –Filtering (e.g., where … ) … although these cannot always be clearly distinguished

XQuery Principles Data Model identical with the XPath data model –documents are ordered, labeled trees –nodes have identity –nodes can have simple or complex types (defined in XML Schema) A query result is an ordered list/sequence of items (nodes, values, attributes, etc., but not lists) –special case: the empty list ()

XQuery Principles (cntd) XQuery can be used without schemas, but can be checked against DTDs and XML schemas XQuery is a functional language –no statements –evaluation of expressions –function definitions –modules

The Recipes DTD (Reminder) <!ATTLIST ingredient name CDATA #REQUIRED amount CDATA #IMPLIED unit CDATA #IMPLIED> <!ATTLIST nutrition calories CDATA #REQUIRED fat CDATA #REQUIRED>

{for $r in doc("recipes.xml")//recipe return $r/title} returns Beef Parmesan with Garlic Angel Hair Pasta Ricotta Pie … A Query over the Recipes Document

Query Features XPath {for $r in doc("recipes.xml")//recipe return $r/title} doc(String) returns input document Part to be returned as it is given{To be evaluated} Iteration $var - variables Sequence of results, one for each variable binding

An Equivalent Stylesheet Template

Features: Summary The result is a new XML document A query consists of parts that are returned as is... and others that are evaluated (everything in {...} ) Calling the function doc( String ) returns an input document XPath is used to retrieve node sets and values Iteration over node sets: for binds a variable to all nodes in a node set Variables can be used in XPath expressions return returns a sequence of results, one for each binding of a variable

XPath is a Fragment of XQuery doc("recipes.xml")//recipe[1]/title returns Beef Parmesan with Garlic Angel Hair Pasta doc("recipes.xml")//recipe[position()<=3] /title returns Beef Parmesan with Garlic Angel Hair Pasta, Ricotta Pie, Linguine alla Pescadora an element a list of elements

Beware: Attributes in XPath doc("recipes.xml")//recipe[1]/ingredient[1] attribute name {"beef cube steak"} string(doc("recipes.xml")//recipe[1] "beef cube steak" an attribute, represented as a constructor for an attribute node (not in Saxon) a value of type string

Beware: Attributes in XPath (cntd.) {string(doc("recipes.xml")//recipe[1] beef cube steak an element with string content

Beware: Attributes in XPath (cntd.) {doc("recipes.xml")//recipe[1] an element with an attribute Note: The XML that we write down is only the surface structure of the data model that is underlying XQuery

Beware: Attributes in XPath (cntd.) <first-ingredient oldName="{doc("recipes.xml")//recipe[1] Beef Beef An attribute is cast as a string

Constructor Syntax For all constituents of documents, there are constructors element first-ingredient { attribute oldName {string(doc("recipes.xml")//recipe[1] "Beef" } equivalent to the notation on the previous slide attribute constructorelement constructor

Iteration with the For-Clause Syntax: for $ var in xpath-expr Example: for $r in doc("recipes.xml")//recipe return string($r) The expression creates a list of bindings for a variable $ var If $ var occurs in an expression exp, then exp is evaluated for each binding For-clauses can be nested: for $r in doc("recipes.xml")//recipe for $v in doc("vegetables.xml")//vegetable return...

What Does This Return? for $i in (1,2,3) for $j in (1,2,3) return element {concat("x",$i * $j)} {$i * $j}

Nested For-clauses: Example {for $r in doc("recipes.xml")//recipe return {for $i in $r//ingredient return } } Returns my-recipes with titles as attributes and my-ingredients with names as text content

The Equivalent Stylesheet Template

The Let Clause Syntax: let $ var := xpath-expr binds variable $ var to a list of nodes, with the nodes in document order does not iterate over the list allows one to keep intermediate results for reuse (not possible in SQL) Example: let $oorecps := doc("recipes.xml")//recipe oil"]

Let Clause: Example {let $oorecps := doc("recipes.xml")//recipe oil"] for $r in $oorecps return {$r/title/text()} {": "} } Calories of recipes with olive oil Note the implicit string concatenation

Let Clause: Example (cntd.) The query returns: Beef Parmesan: 1167 Linguine alla Pescadora: 532

The Where Clause Syntax: where occurs before return clause similar to predicates in XPath comparisons on nodes: = for node equality > for document order Example: for $r in doc("recipes.xml")//recipe where oil" return...

Quantifiers Syntax: some / every $ var in satisfies $ var is bound to all nodes in Test succeeds if is true for some/every binding Note: if is empty, then some is false and all is true

Quantifiers (Example) Recipes that have some compound ingredient Recipes where every top level ingredient is non-compound for $r in doc("recipes.xml")//recipe where some $i in $r/ingredient satisfies $i/ingredient return $r/title for $r in doc("recipes.xml")//recipe where every $i in $r/ingredient satisfies not($i/ingredient) return $r/title

Element Fusion To every recipe, add the attribute calories! {let $rs := doc("recipes.xml")//recipe for $r in $rs return {$r/title} } an elementan attribute

Element Fusion (cntd.) The query result: Beef Parmesan with Garlic Angel Hair Pasta Ricotta Pie Linguine Pescadoro Zuppa Inglese Cailles en Sarcophages

Fusion with Mixed Syntax We mix constructor and XML–Syntax: element result {let $rs := doc("recipes.xml")//recipe for $r in $rs return {attribute calories {$r/title} }

The Same with Constructor Syntax Only element result {let $rs := doc("recipes.xml")//recipe for $r in $rs return element recipe { attribute calories $r/title }

Join condition Pair every ingredient with the recipes where it is used! let $rs := doc("recipes.xml")//recipe for $i in $rs//ingredient for $r in $rs where return {$r/title} Join

Join (cntd.) The query result: Beef Parmesan with Garlic Angel Hair Pasta, Beef Parmesan with Garlic Angel Hair Pasta, Beef Parmesan with Garlic Angel Hair Pasta

Join Exercise Return all pairs of ingredients such that the ingredients have the same name, but occur with different amounts and return the recipes where each of them is used together with the amount being used in those recipes, while returning every pair only once. Could a query for these ingredients be expressed in XPath?

Join condition Document Inversion For every ingredient, return all the recipes where it is used! {let $rs := doc("recipes.xml")//recipe for $i in $rs//ingredient return }

Document Inversion (cntd.) The query result: Zuppa Inglese … Beef Parmesan with Garlic Angel Hair Pasta Linguine Pescadoro …

Eliminating Duplicates The function distinct-values( Node Set ) –extracts the values of a sequence of nodes –creates a duplicate free list of values Note the coercion: nodes are cast as values! Example: let $rs := doc("recipes.xml")//recipe return yields xdt:untypedAtomic("beef cube steak"), xdt:untypedAtomic("onion, sliced into thin rings"),... by the Galax engine

Avoiding Multiple Results in a Join We want that every ingredient is listed only once: Eliminate duplicates using distinct-values! {let $rs := doc("recipes.xml")//recipe for $in in distinct-values( return }

Avoiding Multiple Results (cntd.) The query result: Beef Parmesan with Garlic Angel Hair Pasta Beef Parmesan with Garlic Angel Hair Pasta... Linguine Pescadoro Cailles en Sarcophages...

Syntax: order by expr [ ascending | descending ] for $iname in order by $iname descending return string($iname) yields "whole peppercorns", "whole baby clams", "white sugar",... The Order By Clause

The Order By Clause (cntd.) let $rs := for $r in $rs order by return $r/title In which order will the titles come?

The Order By Clause (cntd.) The interpreter must be told whether the values should be regarded as numbers or as strings (alphanumerical sorting is default) for $r in $rs order by return $r/title Note: –The query returns titles... –but the ordering is according to calories, which do not appear in the output Also possible in SQL! What if combined with distinct-values?

FLWOR Expresssions (pronounced flower) We have now seen the main ingredients of XQuery: For and Let clauses, which can be mixed a Where clause imposing conditions an Order by clause, which determines the order of results a Return clause, which constructs the output. Combining these yields FLWOR expressions.

Conditionals if (expr) then expr else expr Example let $is := doc("recipes.xml")//ingredient for $i in $is[not(ingredient)] let $u := if then attribute unit {"pieces"} else () creates an attribute unit="pieces" if none exists and an empty item list otherwise

We use the conditional to construct variants of ingredients: let $is := doc("recipes.xml")//ingredient for $i in $is[not(ingredient)] let $u := if then attribute {"unit"} {"pieces"} else () return | $u} Conditionals (cntd.) Collects all attributes in a list and adds a unit if needed

Conditionals (cntd.) The query result:,..., …

Exercises Write queries that produce A list, containing for every recipe the recipe's title element and an element with the number of calories The same, ordered according to calories The same, alphabetically ordered according to title The same, ordered according to the fat content The same, with title as attribute and calories as content. A list, containing for every recipe the top level ingredients, dropping the lower level ingredients

Sample Solution 1 A list, containing for every recipe the recipe's title element and an element with the number of calories {for $r in doc("recipes.xml")//recipe return ($r/title, ) } The results returned are 2-element lists. The list constructor is (.,. )

Sample Solution 6 {for $r in doc("recipes.xml")//recipe return {attribute title {$r/title}, for $i in $r/ingredient return if (not($i/ingredient)) then $i else } }

Aggregation Aggregation functions count, sum, avg, min, max Example: The number of recipes with olive oil let $doc := doc("recipes.xml) return {count($doc//recipe = "olive oil"])}

Grouping and Aggregation For each recipe, the number of simple ingredients for $r in doc("recipes.xml")//recipe return {attribute title {$r/title/text()}} {count($r//ingredient[not(ingredient)])}

Grouping and Aggregation (cntd.) The query result: 11, 12, 15, 8, 30

Grouping and Aggregation (cntd.) A list, containing for every ingredient, the number of occurrences of that ingredient let $d := doc("recipes.xml") let $is := return {for $i in $is order by $i return }

Nested Aggregation The recipe with the maximal number of calories! let $rs := doc("recipes.xml")//recipe let $maxCal := for $r in $rs where = $maxCal return string($r/title) returns "Cailles en Sarcophages"

User-defined Functions declare function local:fac($n as xs:integer) as xs:integer { if ($n = 0) then 1 else $n * local:fac($n - 1) }; local:fac(10) Function Declaration Function Call

Example: Nested Ingredients declare function local:nest($n as xs:integer, content as xs:string) as element() { if ($n = 0) then element ingredient{$content} else element ingredient{local:nest($n - 1,$content)} }; local:nest(3,"Stuff")

What Does this Function Return? declare function local:depth($n as node()) as xs:integer { if (fn:empty($n/*)) then 1 else let $cdepths := for $c in $n/* return local:depth($c) return fn:max($cdepths) + 1 };

Exercise Write a function local:element-copy that takes as input a node (= XML tree) produces as output a copy of the tree, but without the attributes