我正在尝试迭代sample书店,结果将每本书退还给您。如何使用XQuery和FLOWR完成此操作?
到目前为止,我最大的努力:
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$
[我希望每个结果都与众不同]。是否可以将结果从一个Xquery链接或管道传递到另一个?我想要的输出将更像是将每本书序列化为xml文件。
数据:
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$
当然可以返回单个结果:
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$
但是,那只是一本书。我将如何通过退还每本书来进行迭代?
由于尚不清楚您希望如何输出,请尝试这样的操作(我已经完成了一些结构)-可能会使您接近最终目的地...
for $x in $db/book
return
(<book-info>
{$x/title/text()}, {$x/author[1]/text()}
</book-info>,' ')
输出:
<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>