简体   繁体   中英

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

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>

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>

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]

Just change

for $ctx in context

to

for $ctx in .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM