how to use Xquery and FLOWR to iterate and return each result?

时光怂恿深爱的人放手 提交于 2019-12-11 10:55:15

问题


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>,'&#10;')

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!