Looking to "rebuild" a book node from the original data.
How is the title data merged, if that's the correct terminology, with the corresponding year data?
Assuming that the title and year are defined with the let operator.
output:
<b>
<t>Everyday Italian Harry Potter XQuery Kick Start Learning XML</t>
<y>2005 2005 2003 2003</y>
</b>
query:
xquery version "3.1";
for $doc in db:open("bookstore")
let $title := data($doc/bookstore/book/title)
let $year := data($doc/bookstore/book/year)
return
<b>
<t>{$title}</t>
<y>{$year}</y>
</b>
data:
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Also tried wrapping the entire query with an element node and curly braces.
output:
<b>
<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
<year>2005</year>
<year>2005</year>
<year>2003</year>
<year>2003</year>
</b>
query:
xquery version "3.1";
<b>
{
for $doc in db:open("bookstore")
return ($doc/bookstore/book/title,$doc/bookstore/book/year)
}
</b>
but, returns as a single element. see also:
nested looping within Xquery results in a mismatch? and idioms
Not sure, but are you looking for something like this?
for $doc in db:open("bookstore")/bookstore/book
return (
for $book in $doc
let $title := data($book/title),
$year := data($book/year)
return(
<b>
<t>{$title}</t>
<y>{$year}</y>)
</b>)
)
Output:
<b>
<t>Everyday Italian</t>
<y>2005</y>)
</b>
<b>
<t>Harry Potter</t>
<y>2005</y>)
</b>
<b>
<t>XQuery Kick Start</t>
<y>2003</y>)
</b>
<b>
<t>Learning XML</t>
<y>2003</y>)
</b>