我如何解决这个在XQUERY 1.0 FLOWR中的autoncremental var case? [英] How can I solve this autoncremental var case in XQUERY 1.0 FLOWR?
问题描述
$ p $
let $ myNode as node():= $ node
for $ ($ path,'/')
$ myNode:= $ myNode / * [name()= $ subpath](:这是无效的行:)
我知道在xQuery 3.0中有一个运算符,我要求xQuery 1.0。
也就是说,你想要什么在 XQuery 1.0 中使用递归函数很容易做到:
declare function local:path
$ context as node()*,
$ steps as xs:string *
)as node()* {
if(empty($ steps))then $ context
else local:path(
$ context / * [name()= $ steps [1]],
$ steps [position()gt 1]
)
};
您可以使用 local:path(文档{< x> < y> foo< / y>< z />< / x>,tokenize(x / y,'/'))
b
在 XQuery 3.0 中,如果没有新的顶级函数,它甚至会更容易:
(
){
$ context / * [name()eq $ step]
},
文档{< x>< y> foo< y>< z />< / x>,
标记大小('x / y','/')
)
函数 fn:fold-left(..)在内部负责递归,并且只需指定如何修改每个步骤。
Pseudocode would be something like:
let $myNode as node() := $node
for $subpath in tokenize($path,'/')
$myNode := $myNode/*[name()=$subpath] (: Here is the invalid line :)
I know there is a operator for this in xQuery 3.0, I am asking for xQuery 1.0.
There's no XQuery 2.0, but what you want to do isn't possible only with a FLWOR expression regardless of version. The set of current nodes would have to be updated between iterations, which isn't how FLWOR expressions work.
That said, what you want to do is easily possible with a recursive function in XQuery 1.0:
declare function local:path(
$context as node()*,
$steps as xs:string*
) as node()* {
if(empty($steps)) then $context
else local:path(
$context/*[name() = $steps[1]],
$steps[position() gt 1]
)
};
You can call it with local:path(document{ <x><y>foo</y><z/></x> }, tokenize("x/y", '/'))
.
In XQuery 3.0 it's even easier to do, without a new top-level function:
fn:fold-left(
function($context, $step) {
$context/*[name() eq $step]
},
document{ <x><y>foo</y><z/></x> },
tokenize('x/y', '/')
)
The function fn:fold-left(..) takes care of the recursion internally and you only have to specify how to modify the context set in each step.
这篇关于我如何解决这个在XQUERY 1.0 FLOWR中的autoncremental var case?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!