XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath...
-
Upload
leon-hutchinson -
Category
Documents
-
view
215 -
download
2
Transcript of XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath...
XQuery
Or, what about REAL databases?
XQuery - its place in the XML team
XLink
XSLT
XQuery
XPath
XPointer
Path expressions in XQuery
The following path expression is used to select all the title elements in the "books.xml" file:
doc("books.xml")/bookstore/book/title(/bookstore selects the bookstore element, /book selects all
the book elements under the bookstore element, and /title selects all the title elements under each book element)
The XQuery above will extract the following:<title lang="en">Everyday Italian</title><title lang="en">Harry Potter</title><title lang="en">XQuery Kick Start</title><title lang="en">Learning XML</title>
Some basic syntax rules of 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 :)
Predicates
XQuery uses predicates to limit the extracted data from XML documents.
The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30:
doc("books.xml")/bookstore/book[price<30]
The XQuery above will extract the following:<book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book>
Using FLWOR
Look at the following path expression:
doc("books.xml")/bookstore/book[price>30]/title
The expression above will select all the title elements under the book elements that is under the bookstore element that have a price element with a value that is higher than 30.
The following FLWOR expression will select exactly the same as the path expression above:
for $x in doc("books.xml")/bookstore/bookwhere $x/price>30return $x/title
The result will be:<title lang="en">XQuery Kick Start</title><title lang="en">Learning XML</title>
FLWORFLWOR is the main engine of XQuery:
For-Let-Where-Order-Return (pronounced "flower") generalizes SELECT-FROM-HAVING-WHERE from SQL
for $d in document("depts.xml")//deptno let $e := document("emps.xml")//employee[deptno = $d] where count($e) >= 10 order by avg($e/salary) descendingreturn<big-dept>{ $d,<headcount>{count($e)}</headcount>,<avgsal>{avg($e/salary)}</avgsal>}</big-dept>
for generates an ordered list of bindings of deptno values to $d let associates to each binding a further binding of the list of emp elements with that department number to $e.A t this stage, we have an ordered list of tuples of bindings: ($d,$e) where filters that list to retain only the desired tuples order sorts that list by the given criteria return constructs for each tuple a resulting value The combined result is in this case a list of departments with at least 10 employees, sorted by average salaries.
Sorting with FLWOR
With FLWOR you can sort the result:
for $x in doc("books.xml")/bookstore/bookwhere $x/price>30order by $x/titlereturn $x/title
The for clause selects all book elements under the bookstore element into a variable called $x.
The where clause selects only book elements with a price element with a value greater than 30.
The order by clause defines the sort-order. Will be sorted by the title element.The return clause specifies what should be returned. Here it returns the title
elements.
The result of the XQuery expression above will be:
<title lang="en">Learning XML</title><title lang="en">XQuery Kick Start</title>
FLWOR and HTML
Now we want to list all the book-titles in our bookstore in an HTML list. We add <ul> and <li> tags to the FLWOR expression, and we want to eliminate the title element, and show only the data inside the title element:
<ul> { for $x in doc("books.xml")/bookstore/book/title order by $x return<li>{data($x})</li> } </ul>
The result of the above will be:<ul> <li>Everyday Italian</li> <li>Harry Potter</li> <li>Learning XML</li> <li>XQuery Kick Start</li></ul>
Extracting attributes<h1>Bookstore</h1><ul>{ for $x in doc("books.xml")/bookstore/book order by $x/title return<li>{data($x/title)}. Category: {data($x/@category)}</li> } </ul></body></html>
The XQuery expression above will generate the following result:
<h1>Bookstore</h1><ul><li>Everyday Italian. Category: COOKING</li><li>Harry Potter. Category: CHILDREN</li><li>Learning XML. Category: WEB</li><li>XQuery Kick Start. Category: WEB</li></ul>
XQuery Conditional Expressions
"If-Then-Else" expressions are allowed in XQuery.
Look at the following example:
for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHILDREN") then <child>{data($x/title)}</child> else <adult>{data($x/title)}</adult>
The result of the example above will be:<adult>Everyday Italian</adult><child>Harry Potter</child><adult>Learning XML</adult><adult>XQuery Kick Start</adult>