问题
I'm looking to iterate a sample bookstore returning each book as a result. How is this accomplished with XQuery and FLOWR?
My best attempt so far:
thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$ basex each.xq
<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" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$ cat each.xq
let $db := db:open("com.w3schools.books")
for $item in $db
return $item/bookstore/book
thufir@dur:~/basex/w3schools$
Only I'd "kinda" like each result differentiated a bit more. Can I chain or pipe results from one Xquery to another perhaps? The output I would want would be more like serializing each book to an xml file.
the data:
thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$ basex all.xq
<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" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$ cat all.xq
let $db := db:open("com.w3schools.books")
return $db
thufir@dur:~/basex/w3schools$
Certainly, possible to return a single result:
thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$ basex singleBook.xq
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>thufir@dur:~/basex/w3schools$
thufir@dur:~/basex/w3schools$ cat singleBook.xq
let $db := db:open("com.w3schools.books")
for $x in $db
return $x/bookstore/book[1]
thufir@dur:~/basex/w3schools$
However, that's just a single book. How would I iterate through returning each book?
回答1:
Since it's not clear exactly how you want the output, try something like this (I made up some structure) - it may get you close to your ultimate destination...
for $x in $db/book
return
(<book-info>
{$x/title/text()}, {$x/author[1]/text()}
</book-info>,' ')
Output:
<book-info>
Everyday Italian, Giada De Laurentiis
</book-info>
<book-info>
Harry Potter, J K. Rowling
</book-info>
<book-info>
XQuery Kick Start, James McGovern
</book-info>
<book-info>
Learning XML, Erik T. Ray
</book-info>
来源:https://stackoverflow.com/questions/58352333/how-to-use-xquery-and-flowr-to-iterate-and-return-each-result