View
228
Download
0
Category
Tags:
Preview:
Citation preview
Basic Syntax
• XQuery vs. XML
• Where do we put…– Curly braces "{" and "} "– Parenthesis " (" and ") "– Square Brackets "[" and "]"– Single quotes: '– Double quotes: "– Angle brackets "<" and ">"
2
When to use a Semicolon ";"
• When do you add a semicolon to the end of a line?
• When do you leave it out?
3
Hello World
xquery version "1.0";
let $message := 'Hello World'
return<results> {$message}</results>
4
6
XQuery’s Nested Structure
• XQueries have a alternating nested structure• Interleave actual XML output and XQuery
instructions
XQueryProcessor
XMLProcessor
Interleaving XML and XQuery
• Use "{" to jump from XML into Xquery
• Use "}" to exit from XQuery
7
<root> {xquery} <tag> {xquery} </tag> {xquery} </root>
Language parser
8
Example of Nested Structure
xquery version “1.0”;let $collection := ‘/db/mycollection’return
<html> <head><title>My Report</title></head><body><table>
{for $i in collection($collection)/itemreturn
<tr> <td> </td> <td> </td></tr>
{$i/name/text()}
}
</table></body></html>
{$i/defintion/text()}
Note that the inner blue XQuery areas always start and end with curly braces { }
9
XQuery FLOWR Expressions
An XQuery FLOWR "expression" has five parts:
• for (optional)
• let (optional)
• order (optional)
• where (rarely used in native XML)
• return (required)
10
Predicates
• Things you add to an XPath expression to limit the selected items
• Like a SQL WHERE clause
• Find all the preferred terms in the glossary
//term[published-indicator=‘true’]
Return all terms that have published-indicator set to be true.
11
XQuery is Concise
• Michael Key "knight's tour" program
• Computes a knight's tour of the chessboard
• Complexity analysis– 276 non-comment lines in XSLT 1.0– 159 non-comment lines in XSLT 2.0– 155 non-comment lines in XQuery
See: http://www.stylusstudio.com/xquerytalk/200503/000537.html
12
XQuery is a “Functional” Language
• XQuery (without the updates) does not change data
• It extracts XML and creates new XML
• Can be highly parallelized like Google’s MapReduce algorithm
http://en.wikipedia.org/wiki/MapReduce
http://en.wikipedia.org/wiki/Functional_programming
[Search YouTube Google Class]
13
Data Types Returned• XQuery can return:
– Text– CSV– Tables– Trees– Graphs
• Serialize options:
See Walmsley p 293
declare option exist:serialize "method=html media-type=text/html indent=yes";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
declare option exist:serialize "method=text media-type=text/text indent=yes";
14
Returning Items in an Ordered List
<ol>{ for $term in collection($collection)/term return <li> {$term/name/text()} </li>}</ol>
This query returns a HTML "Ordered List" of terms.
1. Term 12. Term 23. Term 34. Term 4
15
Example of For Over Collection
for $term in collection('/db/apps/terms/data')/term
let $collection := '/db/apps/terms/data’
for $term in collection($collection)/term
This line may be omitted for clairity
16
Sample XQuery
xquery version "1.0";
(: Example of report on all terms :)
(: make the output XML :)declare option exist:serialize "method=xhtml media-type=text/xml indent=yes";
<terms>{ (: select only xml documents with “term” as the root element :) for $term in collection($collection)/term return <term>{$term/name/text()}</term> }</terms>
17
Sample XQuery that returns XML
xquery version "1.0";
(: make the output XML :)declare option exist:serialize "method=xml media-type=text/xml indent=yes";
let
<terms>{ for $term in collection('/db/apps/terms/data/')/term let $name := $term/name/text() return <term>{$name}</term>}</terms>
Output:
01-xml.xq
18
Restricting Within an XPath
xquery version "1.0";declare option exist:serialize "method=xml media-type=text/xml
indent=yes";
let $collection := '/db/apps/terms/data'
return<results>{ for $term in collection($collection)/term [compare(substring($term/name/text(), 1), ‘a’)] order by $term return $term}</results>
Only find terms that begin with the letter “a”
19
Restricting Rows by Adding a “Where Clause”
<tbody>{ for $term in collection('/db/mdr/glossaries/data')/Term where $term/PublishedIndicator/text() = ‘true' return <tr> <td>{$term/TermName/text()}</td> <td>{$term/Definition/text()}</td> </tr>}</tbody>
for $term in collection('/db/apps/terms/data')/term [$term/PublishedIndicator/text() = ‘true‘]
Square Bracket Notation (always faster in native XML systems):
20
Using Complex Logic in Where Clause
• You can use any number of and/or statements in the where clause
<tbody>{ for $term in collection('/db/mdr/glossaries/data')/Term where $term/ClassifierCode/text() = 'System‘ and $term/AssignedToCode/text() = ‘Dave‘ return <tr> <td>{$term/TermName/text()}</td> <td>{$term/Definition/text()}</td> </tr>}</tbody>
21
Content Management “Macros”<html> <head> {cms:import-css-tables()} </head> <body> {cms:header()} {cms:breadcrumb-glossary()}
• XQuery macros are used to import snippets of canned XML or CSS text
• Change a single location and the entire web site look-and-feel changes
• Similar to server-side includes
22
Selecting Distinct Valuesxquery version "1.0";declare names exist="http://exist.sourceforge.net/NS/exist";declare option exist:serialize "method=html media-type=text/html indent=yes";import module names cms = "http://cms.mdr.danmccreary.com" at "/db/mdr/cms/cms-module.xq";let $title := 'My Report'return<html> <head> <title>{$title}</title> {cms:import-css-tables()} </head> <body> {cms:header()} {cms:breadcrumb-glossary()} <h3>Distinct ClassifierCode Values</h3> <ol>{ for $classifier in distinct-values(collection('/db/mdr/glossaries/data/')/Term/ClassifierCode/text()) return <li>{$classifier}</li> }</ol> </body></html>
23
Sample Results<results>
<assigned-to-code>dave-p</assigned-to-code>
<assigned-to-code>dennis-w</assigned-to-code>
<assigned-to-code>ken-f</assigned-to-code>
<assigned-to-code>kerek-t</assigned-to-code>
</results>
24
Combine Outer Distinct Queryxquery version "1.0";declare names exist="http://exist.sourceforge.net/NS/exist";declare option exist:serialize "method=html media-type=text/html indent=yes";import module names cms = "http://cms.metadata.danmccreary.com" at "/db/mdr/cms/cms-module.xq";let $title := 'My Report'return<html> <head> <title>{$title}</title> {cms:import-css-tables()} </head> <body> {cms:header()} {cms:breadcrumb-glossary()} <ol>{ for $facilitator in distinct-values(collection('/db/mdr/glossaries/data/')/Term/AssignedToCode/text()) return <li><b>Facilitator: </b>{$facilitator}<br/>{ for $term in collection('/db/mdr/glossaries/data/')/Term[AssignedToCode=$facilitator] return $term/TermName/text() }</li> }</ol> </body></html>
25
Subsequence• What to do when you have over 500 items returned by a
query but you only want the first 50 items• Similar to SELECT TOP in SQL
for $term in subsequence($collection, 1, 50)/term return $term
• 1 is the starting term• 50 is the number of term
for $term in subsequence($collection, $start, $length)/term return $term
27
Computation and Transformation
• All computation is a type of transformation• Compilers
– Transform high-level languages into machine language
• Services– Transform an in input request into an output response
• Human Brain– Transform input stimuli into concept recognition via
pattern matching– See On Intelligence by Jeff Hawkins
28
Sample Program
<results> <message>The sum of 47 and 14 is 61</message></results>
xquery version "1.0";
declare option exist:serialize "method=xml media-type=text/xml indent=yes";
let $dan := doc('http://teacher:8080/exist/rest/db/home/dan/data.xml')//number/text()
let $class := doc('http://classroom:8080/exist/rest/db/home/dan/data.xml')//number/text()
let $sum := $dan + $mdr
return
<results>
<message>The sum of {$dan} and {$class} is {$sum}</message>
</results>
29
Suggested Lab
• Use the "XQuery Sandbox" in eXist
• Try running some sample XQueries
• Use oXygen to run some simple queries
Recommended