简体   繁体   English

XQuery递归函数

[英]XQuery Recursive Function

I asked similiar to this question before but I'm still not handled this case correctly, firstly I'm saying that I am new to this XQuery language so configure the recursive strategy is really hard for me for now. 我之前曾问过类似的问题,但是我仍然无法正确处理这种情况,首先,我是说我对这种XQuery语言是陌生的,因此现在对我来说,配置递归策略确实很困难。

XQuery Sample Code to be executed : 要执行的XQuery示例代码:

declare variable $stuff :=
  <doc>
  <embed-session>
    <session>
          <title>Fred Smith</title>
     <session>
     <title>Ahmet Özcan</title>
     <session>
     <title>Erman Bey</title>
     </session>
     </session>
    </session>
    </embed-session>
  </doc>;

declare function local:change($node as node()) as element()
{
  typeswitch($node)
  case element() return 
    element { fn:node-name($node) } {
      attribute title {data($node/title)},
      $node/session ! local:change(.)
    }
  default return $node
};

local:change($stuff/embed-session/session)

The output of the code : 

<session title="Fred Smith">
  <session title="Ahmet Özcan">
    <session title="Erman Bey"/>
  </session>
</session>

It seems that it fits with my expectations but if I add one more embed session node to my stuff variable for example : 看来这符合我的期望,但是例如,如果我在我的东西变量中再添加一个嵌入会话节点,则:

<embed-session>
<session>
<title>Ferhat Bey</title>
</session>
</embed-session>

to my parameter of the function it says that : [XPTY0004] Cannot treat item() sequence as node(): (..., ) 我的函数参数说:[XPTY0004]无法将item()序列视为node():(...,)

The expected output of the code after add the embed-session mentioned below : 添加以下提到的embed-session之后,代码的预期输出:

<session title="Fred Smith">
  <session title="Ahmet Özcan">
    <session title="Erman Bey"/>
  </session>
</session>

<session title="Ferhat Bey"/>

I know it is sequence but why I couldn't assign sequence to node as parameter ? 我知道它是序列,但是为什么我不能将序列分配给node作为参数?

It's always a good idea to declare the expected types of your function arguments. 声明函数参数的预期类型总是一个好主意。 I think you are expecting $node to be a node, but in your function call you are supplying a sequence of nodes. 我认为您期望$ node是一个节点,但是在函数调用中您提供了一系列节点。 element(x) only matches a singleton sequence, so it is not matching when you supply more than one. element(x)仅匹配一个单例序列,因此在提供多个序列时不匹配。 If you had written "$node as node()" then this simple error would have been caught much more quickly, and we would all have saved a lot of time. 如果您编写了“ $ node as node()”,那么这个简单的错误将被更快地捕获,并且我们都将节省很多时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM