XML Query (XQuery)

XML Foundations [./]
Fall 2010 — INFO 242 (CCN 42593)

Erik Wilde, UC Berkeley School of Information
2010-11-18

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)

Contents

E. Wilde: XML Query (XQuery)

(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. XQuery has been built on top of XPath 2.0, which means it uses the same foundation as XSLT 2.0. Both languages have a large overlap, and according to personal preferences and the XML task, one language may be preferred over the other. Features such as user-defined functions and schema-awareness bring XQuery even closer to XSLT 2.0, making the decision to choose one over the other mostly a question of personal preference.



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. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]
XQuery in the XML Landscape E. Wilde: XML Query (XQuery)

(4) XSLT using XPath

XSLT Processing

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

(5) XQuery using XPath

File-based XQuery Processing

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

(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. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]

Syntax Matters

Syntax Matters E. Wilde: XML Query (XQuery)

(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)

(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)

(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)

(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. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]
For Let Where Order Return (FLWOR) E. Wilde: XML Query (XQuery)

(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)

(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)

(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
    • where is comparable to XPath predicates


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

(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)

(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)

(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)

(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)

(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)

(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 attributes
<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>


XQuery & XSLT

Outline (XQuery & XSLT)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]
XQuery & XSLT E. Wilde: XML Query (XQuery)

(24) XPath 2.0 vs. XSLT 2.0



XQuery & XSLT E. Wilde: XML Query (XQuery)

(25) Turing Completeness



XQuery & XSLT E. Wilde: XML Query (XQuery)

(26) XSLT Assumptions



XQuery & XSLT E. Wilde: XML Query (XQuery)

(27) XQuery Assumptions



XQuery & XSLT E. Wilde: XML Query (XQuery)

(28) XSLT BlogXML Processor

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
  <html>
   <body>
    <table border="1" cellpadding="20">
     <xsl:for-each select="//post">
      <tr>
       <td><a href="../img/{image/@src}.jpg"><img src="../img/{image/@src}-small.jpg"/></a></td>
       <td>
        <h2><xsl:value-of select="format-date(@date, '[F] [MNn] [D], [Y]')"/>: <xsl:value-of select="title"/></h2>
        <p><xsl:value-of select="text"/></p>
       </td>
      </tr>
     </xsl:for-each>
    </table>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>


XQuery & XSLT E. Wilde: XML Query (XQuery)

(29) XQuery BlogXML Processor

<html>
 <body>
  <table border="1" cellpadding="20">
   { for $post in //post return
    <tr>
     <td><a href="../img/{ $post/image/@src }.jpg"><img src="../img/{ $post/image/@src }-small.jpg"/></a></td>
     <td>
      <h2> { local:format-date($post/@date) }: { $post/title/text() } </h2>
      <p> { $post/text/text() } </p>
     </td>
    </tr>
   }
  </table>
 </body>
</html>


XQuery & XSLT E. Wilde: XML Query (XQuery)

(30) 2-Step vs. 1-Step



XQuery & XSLT E. Wilde: XML Query (XQuery)

(31) Web App with XQuery & XSLT

Web Application Tiers with XQuery and XSLT

More XQuery Details

Outline (More XQuery Details)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]
More XQuery Details E. Wilde: XML Query (XQuery)

(33) Comma Operator



More XQuery Details E. Wilde: XML Query (XQuery)

(34) Whitespace in XQuery

declare boundary-space strip;
let $a := "Bob Glushko"
return
  <book>
	<title>Document Engineering</title>
	<author> { $a } </author>
  </book>


More XQuery Details E. Wilde: XML Query (XQuery)

(35) Constructors



User-Defined Functions

Outline (User-Defined Functions)

  1. XQuery in the XML Landscape [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
  3. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]
User-Defined Functions E. Wilde: XML Query (XQuery)

(37) Reusable code in XQuery

declare function local:onetwothree() as xs:integer+ { (1, 2, 3) };


User-Defined Functions E. Wilde: XML Query (XQuery)

(38) Local Function

declare function local:format-date($date) { string($date) } ;

<html>
 <body>
  <table border="1" cellpadding="20">
   { for $post in //post return
    <tr>
     <td><a href="../img/{ $post/image/@src }.jpg"><img src="../img/{ $post/image/@src }-small.jpg"/></a></td>
     <td>
      <h2> { local:format-date($post/@date) }: { $post/title/text() } </h2>
      <p> { $post/text/text() } </p>
     </td>
    </tr>
   }
  </table>
 </body>
</html>


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. XQuery & XSLT [8]
  4. More XQuery Details [3]
  5. User-Defined Functions [2]
  6. Conclusions [2]
Conclusions E. Wilde: XML Query (XQuery)

(40) XQuery vs. XSLT



Conclusions E. Wilde: XML Query (XQuery)

(41) Clearer Queries



2010-11-18 XML Foundations [./]
Fall 2010 — INFO 242 (CCN 42593)