XQuery in BaseX - let statement outside of file:write()

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-03 06:40:26

问题


I have the following code:

let $fName := "C:\Users\user\Documents\Sitemaps\Updated Pages\Books.xml"

file:write($fName,
  element titles{
    for $x in doc("http://www.w3schools.com/xsl/books.xml")/bookstore/book
    where $x/price>0
    order by $x/title
    return $x/title
})

BaseX is giving me an error - "Incomplete FLWOR expression: expecting 'return'." It is specifically highlighting the file:write line, underlining file in red.

I'm not sure why this is happening. How do I structure this to avoid syntax errors?


回答1:


If you want to declare constants at the top of the file, you can use declare variable instead of let (which, as @wst said, must be part of a FLWOR expression):

declare variable $fName := "C:\Users\user\Documents\Sitemaps\Updated Pages\Books.xml";
file:write($fName,
...



回答2:


You can't have a let without a return. In your case you would simply add the return before file:write:

let $fName := "C:\Users\user\Documents\Sitemaps\Updated Pages\Books.xml"
return file:write($fName,
...

It's easier to reason about XQuery if you consider that everything is an expression. What you would normally be able to execute as a statement in a procedural language, possibly with no dependencies to other statements, in XQuery must be part of a valid expression - generally, FLWOR or XPath.



来源:https://stackoverflow.com/questions/34905392/xquery-in-basex-let-statement-outside-of-filewrite

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