如何从这家书店归还第三本书?
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]
到目前为止,我只是得到一个空白的结果:
PS C:\ Users \ thufir \ Desktop \ basex> PS C:\ Users \ thufir \ Desktop \ basex> basex contextDoc.xq PS C:\ Users \ thufir \ Desktop \ basex>
答案 0 :(得分:3)
您需要调整 for 子句,并使用点表示法引用上下文,如下所示:
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
输出
<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>
答案 1 :(得分:3)
您的问题标题说的是“使用FLWOR表达式”,但是FLWOR表达式对于这样一个简单的查询来说是过大的。
@YitzhakKhabinsky提供了正确的答案:
for $ctx in ./bookstore/book[3] return $ctx
但是表达式for $X in Y return $X
只是Y
的冗长表达,因此简化为
./bookstore/book[3]
或更简单
bookstore/book[3]
答案 2 :(得分:2)
当您从for循环中进行迭代时,您会在$ ctx变量中获得元素书店。您可以通过写名称($ ctx)进行检查。因此,您无需再次将bookstore元素写为$ ctx / bookstore / book [3]。只需从代码中删除书店,然后将查询写为$ ctx / book [3]。
答案 3 :(得分:1)