XQuery – Part III

XML Foundations [./]
Fall 2011 — INFO 242 (CCN 42596)

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

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: XQuery – Part III

Contents

E. Wilde: XQuery – Part III

(2) Abstract

In this continuation of the XPath/XQuery theme, we look at how XPath 2.0 fits into the greater picture of XML technologies, and how XPath 2.0 and XQuery 1.0 provide language constructs that go way beyonf the rather limited facilities of XPath 1.0.



XPath 2.0 Applications

Outline (XPath 2.0 Applications)

  1. XPath 2.0 Applications [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
XPath 2.0 Applications E. Wilde: XQuery – Part III

(4) Standalone

for $i in ( 11, 22, 33, 'string' ) return ($i, number($i))


XPath 2.0 Applications E. Wilde: XQuery – Part III

(5) XQuery

declare variable $firstName external;
<videos featuring="{$firstName}"> {
  let $doc := .
  for $v in $doc//video, $a in $doc//actors/actor
  where ends-with($a, $firstName) and $v/actorRef = $a/@id
  order by $v/year
  return
	<video year="{$v/year}"> { $v/title } </video> }
</videos>


XPath 2.0 Applications E. Wilde: XQuery – Part III

(6) XSLT 2.0



XQuery Expressions

Outline (XQuery Expressions)

  1. XPath 2.0 Applications [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]

Syntax Matters

Outline (Syntax Matters)

  1. XPath 2.0 Applications [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
Syntax Matters E. Wilde: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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. XPath 2.0 Applications [3]
  2. XQuery Expressions [13]
    1. Syntax Matters [4]
    2. For Let Where Order Return (FLWOR) [9]
For Let Where Order Return (FLWOR) E. Wilde: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

(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: XQuery – Part III

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


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