[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
In this continuation of the XPath/XQuery theme, we look at how XPath 2.0 fits into the greater picture of XML technologies, and how XPath 2.0 and XQuery 1.0 provide language constructs that go way beyonf the rather limited facilities of XPath 1.0.
regular expressions for XML
for $i in ( 11, 22, 33, 'string' ) return ($i, number($i))
declare variable $firstName external; <videos featuring="{$firstName}"> { let $doc := . for $v in $doc//video, $a in $doc//actors/actor where ends-with($a, $firstName) and $v/actorRef = $a/@id order by $v/year return <video year="{$v/year}"> { $v/title } </video> } </videos>
a query is a transformation is a query
<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>