1 / 84

Introduction to XQuery

Introduction to XQuery. Bun Yue Professor, CS/CIS UHCL. W3C Recommendations. http://www.w3.org/TR/xquery/ : W3C XQuery http://www.w3.org/TR/xmlquery-use-cases : XQuery use cases. http://www.w3.org/TR/xquery-operators/ : XQuery and XPath functions.

takoda
Download Presentation

Introduction to XQuery

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to XQuery Bun Yue Professor, CS/CIS UHCL

  2. W3C Recommendations • http://www.w3.org/TR/xquery/: W3C XQuery http://www.w3.org/TR/xmlquery-use-cases: XQuery use cases. • http://www.w3.org/TR/xquery-operators/: XQuery and XPath functions. • http://www.w3.org/TR/xpath-datamodel/: XQuery 1.0 and XPath 2.0 Data Model. • http://www.w3.org/TR/xpath20/: XPath 2.0. • http://www.w3.org/TR/xmlschema-1/: XML Schema Part 1: Structures. • http://www.w3.org/TR/xmlschema-2/: XML Schema Part 2: datatypes.

  3. Introduction • XQuery is designed for effectively query and retrieve information from a diversified XML sources. • The XML sources can be one or more XML documents. • XQuery is derived from Quilt, and has borrowed features from XPath, XQL, SQL, etc.

  4. Introduction • It is a functional language where a query is an expression. • There are three faces of the XQuery languages: • A "surface" syntax that programmers may probably use. • An XML-based syntax that machine may probably use (XQueryX). • A formal semantic that XQuery engine implementators use.

  5. Introduction. • XQuery 1.0 extends XPath 2.0. • The type system of XQuery is based on XML Schema. • A limitation of XQuery: • No update or insert. • The basic building block of XQuery is expressions. (In this sense, like SQL, XQuery is not a full programming language.)

  6. Comparing to SQL

  7. Review of XPath 2.0 • The value of an expression is a sequence, which is an ordered list of items. • An item can be a node or of atomic value. • There are 7 node types: • Document • Element • Attribute • Comment • Text • Processing Instruction • Namespace

  8. XQueryX For doc("census.xml")//person[@job="Athlete"] the corresponding XQueryX can be: <?xml version="1.0"?> <q:query xmlns:q="http://www.w3.org/2001/06/xqueryx">  <q:step q:axis="descendant-or-self">    <q:function q:name="document">      <q:constant q:datatype="xs:string">census.xml</q:constant> </q:function>    <q:predicatedExpr>      <q:identifier>person</q:identifier>      <q:predicate> <q:function q:name="equals"> <q:step q:axis="attribute"> <q:identifier>job</q:identifier>               </q:step>               <q:constant q:datatype="xs:string">Athlete</q:constant>          </q:function>      </q:predicate>    </q:predicatedExpr>  </q:step> </q:query>

  9. Data Types • XQuery is strongly typed. • XQuery types are based on • XML Schema: using the namespace prefix xs and url: http://www.w3.org/2001/XMLSchema. • XPath functions and operators: using the namespace prefix xdt and url: http://www.w3.org/2004/07/xpath-datatypes

  10. Types

  11. Types • xdt:untyped is used to denote element nodes not yet validated. • xdt:untypedAtomic is used to denote atomic types that has not been assigned a more specific type.

  12. Query • A query in XQuery is an expression for • reading XML documents or fragments and • returning a sequence of well-formed XML fragments • Everything in XQuery is an expression that is evaluated to a value.

  13. Query expressions • Some common forms of XQuery expressions are (these appear in most tutorials): • path expressions • element constructors • FLWR or FLOWR (pronounced as "flower") expressions • list expressions • conditional expressions • quantified expressions • datatype expressions

  14. More Queries • Examples of other expressions include: • primary expressions • sequence expressions • arithmetic expressions • logical expressions • comparison expressions • sorting expressions • validate expressions

  15. Comments • XQuery comments are embedded within (: and :).

  16. Functions • Supports a collection of about 200 built-in operators and functions to be used within expressions. • Input functions in XQuery include doc() and collection(). They are used to identify the sources of the XML documents.

  17. Input Functions • Input functions: • doc() • collection().

  18. Prolog • XQuery may have prologs for declarations. Examples: • Variable declarations • Function declarations • Base-URI declarations • Version declarations • Module import • …

  19. Variable Declarations • Format: declare variable $name = expression; • E.g. declare variable $a := doc("census.xml")//person ;

  20. Path Expressions • XQuery 1.0 is a superset of XPath 2.0. • An XPath expression is also an XQuery expression

  21. Editix • Use “View > Windows > XQuery Builder” • For XQ files, use “XSLT/XQuery > Transform using an XQuery Request…” • Specify source xq file, xml file and output file. • Use .xml extension. If you use .txt extension, only text node contents are output.

  22. Examples declare base-uri "whatever-path"; doc("bib.xml")/* Return basically bib.xml.

  23. Example doc("bib.xml")//* Return many nodes (in a sequence). • Results are not well-formed.

  24. Examples doc("bib.xml")//book[@year] count(doc("census.xml")//person)

  25. Element Constructors • Element constructors can be used to construct XML elements. • If the name, attributes, and content of the element are all constants, the element constructor is based on standard XML notation and is called a direct element constructor (W3C).

  26. Example The XQuery <authors> <author>Bun Yue</author> </authors> returns <authors> <author>Bun Yue</author> </authors>

  27. Element Constructors • XQuery expressions can be embedded in the direct element constructors within a pair of curly braces, {}. • For the characters '{' and '}', use '{{' and '}}' respectively. • XQuery expressions may be separated by commas.

  28. Example <authors><author>Bun Yue</author>{ doc("bib.xml")//author }</authors> Adds Bun Yue to the authors of bib.xml.

  29. Computed Constructors • Computed constructors can also be used to declare nodes: • Use the keywords element, attribute, document, text, processing-instruction, comment, or namespace to declare the type of the nodes. • Specify the node names for those node types with names (element, attribute, processing instruction, and namespace nodes) • Use a pair of braces to define the content expressions. • Note the use of commas to separate expressions in the context.

  30. Example (from W3C) element book { attribute isbn {"isbn-0060229357" }, element title { "Harold and the Purple Crayon"}, element author { element first { "Crockett" }, element last {"Johnson" } } }

  31. Example (result) <book isbn="isbn-0060229357">    <title>Harold and the Purple Crayon</title>    <author>      <first>Crockett</first>      <last>Johnson</last>    </author> </book>

  32. Dynamic Element Names • Computed expressions can be used to create elements with dynamic names.

  33. Example <result> { for $author in doc("bib.xml")//author return element {$author/last/text()} { $author/first } } </result>

  34. Example Result <?xml version="1.0" encoding="UTF-8"?> <result> <Stevens> <first>W.</first> </Stevens> <Stevens> <first>W.</first> </Stevens> <Abiteboul> <first>Serge</first> </Abiteboul> <Buneman> <first>Peter</first> </Buneman> <Suciu> <first>Dan</first> </Suciu> </result>

  35. Example • Note that <first> is a child element. See the difference of: <result> { for $author in doc("bib.xml")//author return element {$author/last/text()} { $author/first/text() } } </result>

  36. Example • This example may also result in a runtime error (as the value of <last> may not be suitable for a QName.

  37. FLWOR expressions • FLWOR expressions are one of the most important constructs in XQuery. • You may compare with the SELECT statement of SQL.

  38. FLWOR (W3C) [42]    FLWORExpr    ::=    (ForClause | LetClause)+ WhereClause? OrderByClause? "return" ExprSingle [43]    ForClause    ::=    "for" "$" VarName TypeDeclaration? PositionalVar? "in" ExprSingle ("," "$" VarName TypeDeclaration? PositionalVar? "in" ExprSingle)* [45]    LetClause    ::=    "let" "$" VarName TypeDeclaration? ":=" ExprSingle ("," "$" VarName TypeDeclaration? ":=" ExprSingle)* [123]    TypeDeclaration    ::=    "as" SequenceType [44]    PositionalVar    ::=    "at" "$" VarName [46]    WhereClause    ::=    "where" Expr [47]    OrderByClause    ::=    ("order" "by" | "stable" "order" "by") OrderSpecList [48]    OrderSpecList    ::=    OrderSpec ("," OrderSpec)* [49]    OrderSpec    ::=    ExprSingle OrderModifier [50]    OrderModifier    ::=    ("ascending" | "descending")? (("empty" "greatest") | ("empty" "least"))? ("collation" StringLiteral)?

  39. FLWOR • FLWOR expressions allow: • For: Iteration through items in XPath 2.0 sequences. Create a tuple stream where each tuple contains a distinct binding for each variable to a distinct value. • Let: Variables binding • Where: Predicate application for inclusion in the iteration. • Order by: Ordering data set for the iteration. • Return: Constructing new result for returning.

  40. For and Let • The for and let clauses produces a tuple stream. • A tuple consists of one or more bound variables. • A variable begins with the prefix $. • A bound variable is one that has been assigned a value.

  41. Example declare base-uri “whatever”; let $a := doc("bib.xml")//author return <authors> { $a } </authors>

  42. Example Results <?xml version="1.0" encoding="UTF-8"?> <authors> <author> <last>Stevens</last> <first>W.</first> </author> <author> <last>Stevens</last> <first>W.</first> </author> … </authors>

  43. Example Note • In this example: • The tuple stream is composed of only one tuple. • The variable $b in this tuple is bound to the node sequence of 5 <author> nodes.

  44. Example for $a in doc("bib.xml")//author return <authors> { $a } </authors>

  45. Example Result <?xml version="1.0" encoding="UTF-8"?> <authors> <author> <last>Stevens</last> <first>W.</first> </author> </authors> <authors> <author> <last>Stevens</last> <first>W.</first> </author> </authors> … </authors>

  46. Example Notes • In this example: • The tuple stream is composed of only five tuples. • The variable $b in this tuple is bound to one <author> node at a time.

  47. Example for $a in doc("bib.xml")//author, $b in doc("bib.xml")//author return <count/>

  48. Example Result <?xml version="1.0" encoding="UTF-8"?> <count/> <count/> <count/> <count/> … (: 25 counts :)

  49. Example Note • The tuple stream is composed of only 25 tuples. • The 25 tuples are: • ($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Stevens</last><first>W.</first></author>) • ($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Stevens</last><first>W.</first></author>) • ($a: <author><last>Stevens</last><first>W.</first></author>, $b: <author><last>Abiteboul</last><first>Serge</first></author>) • …

  50. Example for $a in doc("bib.xml")//author, $b in $a/last return <count />

More Related