how to return the third book from this bookstore using FLOWR in XQuery?

百般思念 提交于 2019-12-11 17:13:41

问题


How can I return the third book from this bookstore?

declare context item := document{
<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>
};

let $date := current-date()
for $ctx in context
return $ctx/bookstore/book[3]

so far I'm just getting a blank result:

PS C:\Users\thufir\Desktop\basex> PS C:\Users\thufir\Desktop\basex> basex contextDoc.xq PS C:\Users\thufir\Desktop\basex>


回答1:


Your question title says "using a FLWOR expression", but FLWOR expressions are overkill for such a simple query.

@YitzhakKhabinsky has provided a correct answer:

for $ctx in ./bookstore/book[3] return $ctx

but the expression for $X in Y return $X is just a verbose way of saying Y, so this simplifies to

./bookstore/book[3]

or simpler still

bookstore/book[3]



回答2:


You need to adjust the for clause and refer to the context by using dot notation as follows:

XQuery

declare context item := document {
<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>
};

for $ctx in ./bookstore/book[3]
return $ctx

Output

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



回答3:


When you iterate from for loop you get element bookstore in $ctx variable. you check this by write name($ctx). So you don't need to write again bookstore element as $ctx/bookstore/book[3]. Just remove bookstore from your code and write query as $ctx/book[3].




回答4:


Just change

for $ctx in context

to

for $ctx in .

See this at https://xqueryfiddle.liberty-development.net/b4GWVn



来源:https://stackoverflow.com/questions/58424946/how-to-return-the-third-book-from-this-bookstore-using-flowr-in-xquery

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