[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/]
vs.XQuery
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. 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.
<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>
<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>
vs.XQuery
//reference[starts-with(date/@value, '2000')]/title
for $ref in //reference where starts-with($ref/date/@value, '2000') return $ref/title
flower(it went through several syntax iterations)
for $i in (1 to 10) return $i * $i
<titles> { for $ref in //reference/title/text() return <title> { $ref } </title> } </titles>
<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>
let $x := 'whatever' return …
for $x in 'whatever' return …
<titles> { for $ref in //reference where starts-with($ref/date/@value, '2000') return $ref/title } </titles>
<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>
<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>
<?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>