如何使用Xquery和FLOWR迭代并返回每个结果?

时间:2019-10-12 08:34:07

标签: xml xpath xml-parsing xquery basex

我正在寻找一个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$ 

但是,那只是一本书。我将如何通过退还每本书来进行迭代?

1 个答案:

答案 0 :(得分:2)

由于尚不清楚您希望如何输出,请尝试这样的操作(由我组成一些结构)-可能会使您接近最终目的地...

for $x in $db/book
return 
(<book-info>
{$x/title/text()}, {$x/author[1]/text()}
</book-info>,'&#10;')

输出:

<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>