XML Query (XQuery) – Part I

XML Foundations [./]
Fall 2009 — INFO 242 (CCN 42575)

Erik Wilde, UC Berkeley School of Information
2009-11-10

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: XML Query (XQuery) – Part I

Contents

E. Wilde: XML Query (XQuery) – Part I

(2) Abstract

The XML Query (XQuery) language has been designed to query collections of XML documents. It is thus different from XSLT, which primarily transforms one document at a time. However, the core of both languages is XPath 2.0, which means that learning XQuery (and XSLT 2.0) is not very hard when starting with a solid knowledge of XPath 2.0. XQuery's main concept is an expression language which supports iteration and binding of variables to intermediate results. The final result of an XQuery is a tree, which can be serialized in various serialization formats.



XQuery in the XML Landscape

Outline (XQuery in the XML Landscape)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. Processing Model [2]
  4. Conclusions [1]
XQuery in the XML Landscape E. Wilde: XML Query (XQuery) – Part I

(4) XSLT using XPath

XSLT Processing

XQuery in the XML Landscape E. Wilde: XML Query (XQuery) – Part I

(5) XQuery using XPath

File-based XQuery Processing

XQuery in the XML Landscape E. Wilde: XML Query (XQuery) – Part I

(6) XQuery using XPath + Database

DB-based XQuery Processing

XQuery Expressions

Outline (XQuery Expressions)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. Processing Model [2]
  4. Conclusions [1]

Syntax Matters

Syntax Matters E. Wilde: XML Query (XQuery) – Part I

(9) XML Syntax

  • XML syntax is good (and bad …)
    • XML is well-known and supported by many tools
    • XML is verbose and hard to parse for humans
  • XSLT's XML syntax is good (and bad …)
    • it is easy to use for experienced XML users
    • and a nightmare to get used to for XML beginners
    • it can be easily fed to itself for advanced applications (meta-stylesheets)
    • which only advanced XSLT programmers will ever do
  • Even XSLT is not completely XMLized
    • XSLT language constructs are XML elements and attributes
    • XPath expressions use their own non-XML syntax (hard to process in XSLT )
    • XSLT is a compromise between XML and non-XML


Syntax Matters E. Wilde: XML Query (XQuery) – Part I

(10) XQuery Syntax

  • XQuery should become the new query language for data
    • some say for any data (because relational data can be represented as XML anyway)
    • others say for XML data only
    • either way, XQuery will become an important language
  • XML syntax has been perceived as something people don't like
    • there is no hard evidence that XML syntax really is an acceptance problem
    • but many people think this might be the case for a query language
    • non-XML syntaxes can be more compact than XML syntaxes
  • Query language specialists are used to SQL's keyword-based syntax
    • XQuery imitates this approach to make it easier to switch
    • XML shows up anyway because XQuery constructs XML
    • you can't really escape XML when you are working with XML …


Syntax Matters E. Wilde: XML Query (XQuery) – Part I

(11) XQuery Syntax Example

<bib>
 {
  for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
  where $b/publisher = "Addison-Wesley" and $b/@year > 1991
  return
    <book year="{ $b/@year }">
     { $b/title }
    </book>
 }
</bib>


Syntax Matters E. Wilde: XML Query (XQuery) – Part I

(12) XQueryX Syntax Example

     <xqx:flworExpr>
      <xqx:forClause>
       <xqx:forClauseItem>
        <xqx:typedVariableBinding>
         <xqx:varName>b</xqx:varName>
        </xqx:typedVariableBinding>
        <xqx:forExpr>
         <xqx:pathExpr>
          <xqx:stepExpr>
           <xqx:filterExpr>
            <xqx:functionCallExpr>
             <xqx:functionName>doc</xqx:functionName>
             <xqx:arguments>
              <xqx:stringConstantExpr>
               <xqx:value>http://bstore1.example.com/bib.xml</xqx:value>
              </xqx:stringConstantExpr>
             </xqx:arguments>
            </xqx:functionCallExpr>
           </xqx:filterExpr>
          </xqx:stepExpr>
          <xqx:stepExpr>
           <xqx:xpathAxis>child</xqx:xpathAxis>
           <xqx:nameTest>bib</xqx:nameTest>
          </xqx:stepExpr>
          <xqx:stepExpr>
           <xqx:xpathAxis>child</xqx:xpathAxis>
           <xqx:nameTest>book</xqx:nameTest>


For Let Where Order Return (FLWOR)

Outline (For Let Where Order Return (FLWOR))

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. Processing Model [2]
  4. Conclusions [1]
For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(14) XPath and XQuery

  • Every XPath expression is a valid XQuery expression
    • if you are good at XPath, you can reuse a lot of your knowledge
    • XQuery provides alternative expressions and missing functionality
  • XPath is a language for selecting nodes in existing documents
    • XPath has no language features to construct new XML trees
    • re-ordering a tree (i.e., sorting) also involves constructing a new tree
  • XPath is more relevant for XSLT/XQuery experts
    • advanced XPath concepts will take you very far
  • XQuery expressions can be used to simplify XPaths
    • in the same way as in XSLT, there is a trade-off in complexity between the languages


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(15) XPath vs. XQuery

  • Choosing a certain style can be a question of taste
//reference[starts-with(date/@value, '2000')]/title
  for $ref in //reference
   where starts-with($ref/date/@value, '2000')
   return $ref/title


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(16) XQuery's Central Construct

  • FLWOR is pronounced flower (it went through several syntax iterations)
    • must have at least one for or let
    • may have where and order by
    • must have return
  • Iteration over a sequence of items
    • similar to for-each or similar loop constructs
    • works slightly differently because of XQuery's Processing Model [Processing Model (1)]
    • where is comparable to XPath predicates


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(17) FLWOR – For

  • Iteration over an input sequence
    • for each item in the input sequence, a result is calculated
    • the result of the for clause is the concatenation of all these results
  • If there is only a return, it is the same as XPath's Iterations [XML Path Language (XPath) 2.0; Iterations (1)]
for $i in (1 to 10) return $i * $i
<titles>
 {
  for $ref in //reference/title/text()
   return <title> { $ref } </title>
 }
</titles>


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(18) FLWOR – Let

  • Declares a variable and assigns a value to it
    • may use previously assigned variables
    • can be used any number of times mixed in any order with for clauses
<titles>
 {
  let $title := //reference/title/text()
   return <title> { $title } </title>
 }
</titles>
<titles>
 {
  for $ref in //reference
   let $title := $ref/title/text()
   return <title> { $title } </title>
 }
</titles>


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(19) For & Let

  • Variables in most cases are used for convenience
    • they avoid repeating things and often result in a more structured query expression
    • writing XQueries should not be an exercise in minimizing the character count
  • For single items, for and let can be interchanged (handy for XPath)
let $x := 'whatever' return …
for $x in 'whatever' return …


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(20) FLWOR – Where

  • Filtering the items from for and let clauses
    • there may be at most one where clause in a FLWOR expression
    • it has to appear after all for and let clauses
  • A where clause almost always uses a variable
    • they act as filters of the sequences generated by for clauses
    • they act based on the value of the items in that sequence
<titles>
 {
  for $ref in //reference
   where starts-with($ref/date/@value, '2000')
   return $ref/title
 }
</titles>
  • Predicates in XPath expressions do the exact same thing
    • but predicates can only be applied to nodes
    • SQL users find the where approach easier to understand


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(21) FLWOR – Order by

  • FLWOR results are evaluated in the order resulting from the for clauses
    • for nested clauses, this means a nested evaluation of for clauses
    • this will often reflect the document order (depending on the for XPaths)
  • Any order other than this has to be achieved by sorting
    • multiple sort keys can be specified separated by commas
    • sorting can be done ascending or descending
<names>
 {
  for $person in //person
   let $given := $person/givenname/text()
   let $sur := $person/surname/text()
   order by $given ascending, $sur descending
   return <name> { $given, ' ', $sur } </name>
 }
</names>


For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery) – Part I

(22) FLWOR – Return

  • Required in every FLWOR expression
  • Constructs new nodes and fills them with values
    • element constructors can be used to generate elements
    • attribute constructors can be used to generate elements
<names>
 {
  for $person in //person
   let $given := $person/givenname/text()
   let $sur :=  $person/surname/text()
   order by $given ascending, $sur descending
   return <name given="{ $given }" sur="{ $sur }"/>
 }
</names>


Processing Model

Outline (Processing Model)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. Processing Model [2]
  4. Conclusions [1]
Processing Model E. Wilde: XML Query (XQuery) – Part I

(24) Tuple Stream



Processing Model E. Wilde: XML Query (XQuery) – Part I

(25) Serialization



Conclusions

Outline (Conclusions)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. Processing Model [2]
  4. Conclusions [1]
Conclusions E. Wilde: XML Query (XQuery) – Part I

(27) Clearer Queries



2009-11-10 XML Foundations [./]
Fall 2009 — INFO 242 (CCN 42575)