我如何解决这个在XQUERY 1.0 FLOWR中的autoncremental var case? [英] How can I solve this autoncremental var case in XQUERY 1.0 FLOWR?

查看:212
本文介绍了我如何解决这个在XQUERY 1.0 FLOWR中的autoncremental var case?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伪代码可能类似于:

$ p $ let $ myNode as node():= $ node
for $ ($ path,'/')
$ myNode:= $​​ myNode / * [name()= $ subpath](:这是无效的行:)

我知道在xQuery 3.0中有一个运算符,我要求xQuery 1.0。

解决方案 不存在 XQuery 2.0 ,但是不管版本如何,您只想使用 FLWOR 表达式。当前节点的集合将不得不在迭代之间进行更新,这不是表达式的工作方式。

也就是说,你想要什么在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆