XML Query (XQuery)

Web-Based Publishing (INFO 290-19)

Erik Wilde, UC Berkeley School of Information
2007-02-15
Creative Commons License

This work is licensed under a Creative Commons
Attribution-NonCommercial-ShareAlike 2.5 License.

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.

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. AJAXLink and XQuery [3]
  5. Conclusions [1]

XSLT using XPath

XSLT Processing

XQuery using XPath

File-based XQuery Processing

XQuery using XPath + Database

DB-based XQuery Processing

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. AJAXLink and XQuery [3]
  5. Conclusions [1]

Outline (Syntax Matters)

  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. AJAXLink and XQuery [3]
  5. Conclusions [1]

XML Syntax

XQuery Syntax

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>

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>

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. AJAXLink and XQuery [3]
  5. Conclusions [1]

XPath and XQuery

XPath vs. XQuery

//reference[starts-with(date/@value, '2000')]/title
  for $ref in //reference
   where starts-with($ref/date/@value, '2000')
   return $ref/title

XQuery's Central Construct

FLWOR – For

for $i in (1 to 10) return $i * $i
<titles>
 {
  for $ref in //reference/title/text()
   return <title> { $ref } </title>
 }
</titles>

FLWOR – Let

<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

let $x := 'whatever' return …
for $x in 'whatever' return …

FLWOR – Where

<titles>
 {
  for $ref in //reference
   where starts-with($ref/date/@value, '2000')
   return $ref/title
 }
</titles>

FLWOR – Order by

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

FLWOR – Return

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

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. AJAXLink and XQuery [3]
  5. Conclusions [1]

Tuple Stream

Serialization

Outline (AJAXLink and XQuery)

  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. AJAXLink and XQuery [3]
  5. Conclusions [1]

XPath in AJAXLink

XQuery and XSLT

AJAXLink using XQuery and XSLT

Linklets

AJAXLink with Linkbase-Supplied XSLT

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. AJAXLink and XQuery [3]
  5. Conclusions [1]

Clearer Queries