Skip to content

Instantly share code, notes, and snippets.

@derickson
Created May 3, 2012 23:42
Show Gist options
  • Save derickson/2590465 to your computer and use it in GitHub Desktop.
Save derickson/2590465 to your computer and use it in GitHub Desktop.
Learning XQuery
let $books :=
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
... other books from W3Schools example ...
</bookstore>
return
xdmp:document-insert("books.xml", $books)
("a", "b", "c, "d")
("a", (), "b", "c", "d")
("a", ( "b", "c"), "d")
1 (: a sequence of 1 xs:integer literal :)
"Hello World" (: a sequence of 1 xs:string literal :)
(: returns a sequence of perfect squares :)
for $i in (1 to 10)
return
$i * $i
(: IF statement inside FLWOR statements :)
for $x in (1 to 10)
return
if($x ne 2) then
$x * $x
else
"the number of years I have loved XQuery"
(: Some of the locations in FWLOR that could be considered sequences :)
for $x in SEQUENCE
let $y := SEQUENCE
return
SEQUENCE
(: Sequences inside IF/ELSEs :)
if( BooleanExpression ) then
SEQUENCE
else
SEQUENCE
(: Two statements expressed as a sequence :)
(
for $book in fn:doc("books.xml")//book
return
fn:concat( $book/title, ": ", $book/author),
if(fn:exists(//book[fn:contains(./title, "SQL")]) then
"Yuck"
else
"Hooray!"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment