XML Query (XQuery) – Part II

XML Foundations (INFO 242)

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

This work is licensed under a CC
Attribution 3.0 Unported License

Abstract

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.

Outline (XQuery & XSLT)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]

XPath 2.0 vs. XSLT 2.0

Turing Completeness

XSLT Assumptions

XQuery Assumptions

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

2-Step vs. 1-Step

Web App with XQuery & XSLT

Web Application Tiers with XQuery and XSLT

Outline (More XQuery Details)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]

Comma Operator

Whitespace in XQuery

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

Constructors

Outline (User-Defined Functions)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]

Reusable code in XQuery

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

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>

Outline (Conclusions)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]

XQuery vs. XSLT