2016-06-11 78 views
0
declare variable $testseq as item()* :=(); 

declare function local:insertseq($target as item()*, $position as xs:integer?, $inserts as item()*) 
as item()* (:might be great if we have a keyword to represent nothing:) 
{ 
    fn:insert-before($target, 1, $inserts) (:change the global sequence:) 
() (:simulate returning nothing, empty sequence:) 
}; 

element test 
{ 
    attribute haha {"&"}, 
    local:insertseq($testseq, 1, ('a', 'b')), 
    $testseq 
} 

我需要在脚本运行时将某些东西收集到全局序列中。在脚本结尾我释放序列。函数insertseq必须不返回任何内容。 XQuery有可能吗?或者还有其他的技巧可以做到吗?从BaseXXQuery:声明函数不返回

错误:

$ basex test.xqy 
Stopped at /Users/jack/Documents/SHK/XSD2OWL/Workspace/xqy/test.xqy, 7/4: 
[XPTY0004] Item expected, sequence found: ("a", "b"). 
+0

XQuery是一种功能性语言。即使你使用'insert-before'也是不正确的:它不会修改任何内容,它会返回一个新的序列。你需要重新思考你在输入/输出方面做了什么;如果在处理结束时需要一个序列,那么该序列将需要成为返回值的一部分。 –

+0

某些实现有一个“脚本”扩展,其中有可变变量 – BeniBela

回答

2

您的原始问题的标题的答案实际上是:

declare function local:f() as empty-sequence() { 
() 
}; 

正如你可能想解决一个具体的问题,你能想到的关于创建一个带有另一个标题和相应问题描述的新问题(包括预期输入和输出的一个小例子)。

在函数式语言(如XQuery)中,变量一旦定义就不能重新分配(请参见Referential Transparency)。因此,您需要使用递归函数为序列重复添加值。 fn:fold-left也可以使用:第一次使用时感觉有挑战性,但一旦你明白它的作用,你不想错过。