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