XML Query (XQuery) – Part II

XML Foundations [./]
Fall 2008 — INFO 242 (CCN 42572)

Erik Wilde, UC Berkeley School of Information
2008-12-02

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 II

Contents

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

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



XQuery & XSLT

Outline (XQuery & XSLT)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]
XQuery & XSLT E. Wilde: XML Query (XQuery) – Part II

(4) XPath 2.0 vs. XSLT 2.0



XQuery & XSLT E. Wilde: XML Query (XQuery) – Part II

(5) Turing Completeness



XQuery & XSLT E. Wilde: XML Query (XQuery) – Part II

(6) XSLT Assumptions



XQuery & XSLT E. Wilde: XML Query (XQuery) – Part II

(7) XQuery Assumptions



XQuery & XSLT E. Wilde: XML Query (XQuery) – Part II

(8) 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) – Part II

(9) 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) – Part II

(10) 2-Step vs. 1-Step



XQuery & XSLT E. Wilde: XML Query (XQuery) – Part II

(11) Web App with XQuery & XSLT

Web Application Tiers with XQuery and XSLT

More XQuery Details

Outline (More XQuery Details)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]
More XQuery Details E. Wilde: XML Query (XQuery) – Part II

(13) Comma Operator



More XQuery Details E. Wilde: XML Query (XQuery) – Part II

(14) 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) – Part II

(15) Constructors



User-Defined Functions

Outline (User-Defined Functions)

  1. XQuery & XSLT [8]
  2. More XQuery Details [3]
  3. User-Defined Functions [2]
  4. Conclusions [1]
User-Defined Functions E. Wilde: XML Query (XQuery) – Part II

(17) Reusable code in XQuery

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


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

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

(20) XQuery vs. XSLT



2008-12-02 XML Foundations [./]
Fall 2008 — INFO 242 (CCN 42572)