[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/]
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.
<?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>
<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>
<a>123</a>, <b>456</b>
doc("books.xml")//author, doc("books.xml")//title
for $b in doc("books.xml")//book return $b/title, $b/author
for $b in doc("books.xml")//book return ( $b/title, $b/author )
declare boundary-space strip; let $a := "Bob Glushko" return <book> <title>Document Engineering</title> <author> { $a } </author> </book>
<book> <title>Document Engineering</title> <author>Bob Glushko</author> <price currency="USD">29.99</price> </book>
element "book" { element "title" { "Document Engineering" }, element "author" { "Bob Glushko" }, element "price" { attribute "currency" { "USD" }, 29.99 } }
declare function local:onetwothree() as xs:integer+ { (1, 2, 3) };
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>