XQuery FLOWR Expressions Lecture 35

Similar documents
XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

The Critical-Path Algorithm

Minimal Spanning Trees

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Relational Databases

The Decreasing-Time Algorithm

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Insertions, Deletions, and Updates

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

XQuery Constructors and Joins Lecture 36

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The Traveling Salesman Problem Brute Force Method

The Pairwise-Comparison Method

LR Parsing - Conflicts

PHP Queries and HTML Forms Lecture 23

List Iterator Implementation

Friends and Unary Operators

The Class Construct Part 1

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Solving Recursive Sequences by Iteration

Magnification and Minification

Scope and Parameter Passing

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

The Graphics Pipeline

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

Scheduling and Digraphs

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

DTDs and XML Attributes

The Plurality-with-Elimination Method

Introduction to Databases

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Scope and Parameter Passing

The Coefficient of Determination

Implementing Linked Lists

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Relational Databases Lecture 2

Recursive Descent Parsers

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Programming Languages

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The Graphics Pipeline

Binary Tree Applications

Recognition of Tokens

Introduction to Databases

Recursive Linked Lists

Dynamic Allocation of Memory

Density Curves Sections

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Stacks and their Applications

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

XSLT. Lecture 38. Robb T. Koether. Mon, Apr 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XSLT Mon, Apr 21, / 26

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

The CYK Parsing Algorithm

Function Definition Syntax Tree

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Displaying Distributions - Quantitative Variables

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Ambient and Diffuse Light

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Programming Languages

Street-Routing Problems

Building the Abstract Syntax Trees

The Normal Distribution

Binary Tree Implementation

Introduction to Compiler Design

LR Parsing - The Items

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Binary Tree Implementation

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

Basic PHP Lecture 17

XML and AJAX Lecture 28

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

The Class Construct Part 2

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

The x86 Architecture

Basic CSS Lecture 17

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

Dynamic Allocation of Memory

Transcription:

XQuery FLOWR Expressions Lecture 35 Robb T. Koether Hampden-Sydney College Fri, Apr 13, 2012 Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 1 / 33

1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 2 / 33

Outline 1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 3 / 33

What is XQuery? XQuery is a method of expressing queries that will extract data from an XML file. Each query evaluates to an XML element or a sequence of XML elements. The strength of XQuery over XPath lies in its ability to express queries as FLOWR expressions. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 4 / 33

What is XQuery? FLOWR stands for the keywords for let order by where return Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 5 / 33

Outline 1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 6 / 33

return Statements The return Statement return XPath_expression The return statement will return the sequence of elements represented by the path expression. A return statement is mandatory in every XQuery expression. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 7 / 33

return Statements The return Statement return doc("library.xml")//lname The above statement will return all <lname> elements. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 8 / 33

Outline 1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 9 / 33

let Statements The let Statement let variable := XPath_expression The let statement will assign to a variable the sequence of items described by a path expression. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 10 / 33

let Statements The let Statement let $x := doc("company.xml")//lname return $x The above statement will assign to $x the sequence of all <lname> elements. The return statement will return that sequence. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 11 / 33

let Statements The let Statement let $x := doc("company.xml")//dep_name return data($x/../../../lname) Even though we select nodes at one level, we still have access to the parent nodes. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 12 / 33

Outline 1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 13 / 33

for Statements The for Statement for variable in XPath_expression A for statement will cause a variable to iterate through all the elements in the sequence of elements returned by the path expression. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 14 / 33

for Statements The for Statement for $x in doc("company.xml")//lname return $x The above for statement will iterate through the sequence of <lname> elements. The return statement will return each element individually (not as a sequence). Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 15 / 33

let Statements for vs. let for $x in doc("company.xml")//dep_name return data($x/../../../lname) How does this query differ from a very similar earlier query that used let? Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 16 / 33

for Statements The for Statement for $x in doc("company.xml")//employee let $y := $x/salary return data($y) The above for statement will iterate through the sequence of <employee> elements and select the salary of each employee. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 17 / 33

Outline 1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 18 / 33

where Statements The where Statement where predicate The where clause may follow a path expression. It will filter out all elements that to not satisfy the predicate. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 19 / 33

where Statements The where Statement for $x in doc("company.xml")//employee where $x/salary > 60000 return $x In a for loop, the predicate is applied to each of the values of the loop variable. The for loop will skip those values of the loop variable that do not satisfy the predicate. The above query will return the employee elements of all employees making more than $60,000. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 20 / 33

where Statements The where Statement let $x := doc("company.xml")//employee where $x/salary > 60000 return $x Outside of a for loop, the values of the variable are not iterated, but are treated as one sequence. The predicate is applied to the entire sequence. The above query will return all employee elements because there is at least one who makes more than $60,000. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 21 / 33

where Statements where vs. an XPath Predicate for $x in doc("company.xml")//employee[salary > 60000] return $x In this example, the variable iterates through only the employees who make more than $60,000. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 22 / 33

where Statements where vs. an XPath Predicate let $x := doc("company.xml")//employee[salary > 60000] return $x In this example, the variable is assigned the sequence of elements only of the employees who make more than $60,000. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 23 / 33

for vs. let In a for statement, the variable iterates through the sequence of elements, one at a time. In a let statement, the variable is assigned the entire sequence. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 24 / 33

for vs. let for vs. let for $x in doc("company.xml")//dep_name return data($x/../../../lname) How does this query differ from a very similar earlier query that used let? Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 25 / 33

Outline 1 XQuery 2 return Statements 3 let Statements 4 for Statements 5 where Statements 6 order by Statements Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 26 / 33

order by Statements The order by Statement order by XPath_expression The order by clause will arrange the elements of a variable into a specified order. The path expression must take on one value per item being sorted. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 27 / 33

order by Statements The order by Statement for $x in doc("company.xml")//employee order by $x/sex return data($x/fname) The above expression will return the first names of the employees, with the females first and then the males. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 28 / 33

order by Statements Order by Multiple Fields for $x in doc("company.xml")//employee order by $x/sex, $x/lname return data($x/concat(fname, " ", lname, ",")) The above expression will return the full names, with the females first and then the males, each group alphabetized by last name. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 29 / 33

order by Statements Descending Order for $x in doc("company.xml")//employee order by count($x/*/dependent) descending return data($x/concat(fname, " ", lname, ",")) The above expression will list the names of the employees, sorted in descending order by the number of dependents. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 30 / 33

order by Statements Missing Elements for $x in doc("library.xml")//collection order by $x/authors/author/lname return data($x/title) Notice that the collections without authors are listed first. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 31 / 33

order by Statements Missing Elements empty greatest; empty least; We may append empty greatest or empty least to the order by clause. The default is empty least. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 32 / 33

order by Statements Missing Elements for $x in doc("library.xml")//collection order by $x/authors/author/lname empty greatest return data($x/title) Now, Mark Twain s collection is listed first. Robb T. Koether (Hampden-Sydney College) XQuery FLOWR ExpressionsLecture 35 Fri, Apr 13, 2012 33 / 33