[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. The final result of an XQuery is a tree, which can be serialized in various serialization formats.



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