Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 XQuery Syntax Dan McCreary May, 2011. Basic Syntax XQuery vs. XML Where do we put… –Curly braces "{" and "} " –Parenthesis " (" and ") " –Square Brackets.

Similar presentations


Presentation on theme: "1 XQuery Syntax Dan McCreary May, 2011. Basic Syntax XQuery vs. XML Where do we put… –Curly braces "{" and "} " –Parenthesis " (" and ") " –Square Brackets."— Presentation transcript:

1 1 XQuery Syntax Dan McCreary May, 2011

2 Basic Syntax XQuery vs. XML Where do we put… –Curly braces "{" and "} " –Parenthesis " (" and ") " –Square Brackets "[" and "]" –Single quotes: ' –Double quotes: " –Angle brackets " " 2

3 When to use a Semicolon ";" When do you add a semicolon to the end of a line? When do you leave it out? 3

4 Hello World xquery version "1.0"; let $message := 'Hello World' return {$message} 4

5 When can we return An XML document – A sequence –(1, 2, 3) Either? 5

6 6 XQuery’s Nested Structure XQueries have a alternating nested structure Interleave actual XML output and XQuery instructions XQuery Processor XML Processor

7 Interleaving XML and XQuery Use "{" to jump from XML into Xquery Use "}" to exit from XQuery 7 {xquery} {xquery} {xquery} Language parser

8 8 Example of Nested Structure xquery version “1.0”; let $collection := ‘/db/mycollection’ return My Report {for $i in collection($collection)/item return {$i/name/text()} } {$i/defintion/text()} Note that the inner blue XQuery areas always start and end with curly braces { }

9 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 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 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 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 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 14 Returning Items in an Ordered List { for $term in collection($collection)/term return {$term/name/text()} } This query returns a HTML "Ordered List" of terms. 1.Term 1 2.Term 2 3.Term 3 4.Term 4

15 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 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"; { (: select only xml documents with “term” as the root element :) for $term in collection($collection)/term return {$term/name/text()} }

17 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 { for $term in collection('/db/apps/terms/data/')/term let $name := $term/name/text() return {$name} } Output: 01-xml.xq

18 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 { for $term in collection($collection)/term [compare(substring($term/name/text(), 1), ‘a’)] order by $term return $term } Only find terms that begin with the letter “a”

19 19 Restricting Rows by Adding a “Where Clause” { for $term in collection('/db/mdr/glossaries/data')/Term where $term/PublishedIndicator/text() = ‘true' return {$term/TermName/text()} {$term/Definition/text()} } for $term in collection('/db/apps/terms/data')/term [$term/PublishedIndicator/text() = ‘true‘] Square Bracket Notation (always faster in native XML systems):

20 20 Using Complex Logic in Where Clause You can use any number of and/or statements in the where clause { for $term in collection('/db/mdr/glossaries/data')/Term where $term/ClassifierCode/text() = 'System‘ and $term/AssignedToCode/text() = ‘Dave‘ return {$term/TermName/text()} {$term/Definition/text()} }

21 21 Content Management “Macros” {cms:import-css-tables()} {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 22 Selecting Distinct Values xquery 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 {$title} {cms:import-css-tables()} {cms:header()} {cms:breadcrumb-glossary()} Distinct ClassifierCode Values { for $classifier in distinct-values(collection('/db/mdr/glossaries/data/')/Term/ClassifierCode/text()) return {$classifier} }

23 23 Sample Results dave-p dennis-w ken-f kerek-t

24 24 Combine Outer Distinct Query xquery 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 {$title} {cms:import-css-tables()} {cms:header()} {cms:breadcrumb-glossary()} { for $facilitator in distinct-values(collection('/db/mdr/glossaries/data/')/Term/AssignedToCode/text()) return Facilitator: {$facilitator} { for $term in collection('/db/mdr/glossaries/data/')/Term[AssignedToCode=$facilitator] return $term/TermName/text() }

25 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

26 26 Try it out http://localhost:8080/exist/rest/db/apps/terms/ind ex.xhtml

27 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 28 Sample Program The sum of 47 and 14 is 61 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 The sum of {$dan} and {$class} is {$sum}

29 29 Suggested Lab Use the "XQuery Sandbox" in eXist Try running some sample XQueries Use oXygen to run some simple queries

30 30 Questions?


Download ppt "1 XQuery Syntax Dan McCreary May, 2011. Basic Syntax XQuery vs. XML Where do we put… –Curly braces "{" and "} " –Parenthesis " (" and ") " –Square Brackets."

Similar presentations


Ads by Google