Marklogic 一个递归方法的实现

来源:互联网 发布:python 爬虫 伯乐在线 编辑:程序博客网 时间:2024/05/22 14:06

以下代码来自于roxy 中关于deployment 中的流程部分。

declare function setup:add-phrase-throughs-R(

  $admin-configaselement(configuration),

  $databaseas xs:unsignedLong,

  $phrase-throughsaselement(db:phrase-through)*) aselement(configuration)

{

  if($phrase-throughs) then

    setup:add-phrase-throughs-R(

      admin:database-add-phrase-through($admin-config,$database, $phrase-throughs[1]),

      $database,

      fn:subsequence($phrase-throughs,2))

  else

    $admin-config

};

逻辑代码很简单,就是一个递归来实行将element-phrase-through建立到数据库中去。

解释:

add-phrase-throughs-R:是一个递归的方法。

fn:subsequence($phrase-throughs,2))表示从$phrase-throughs中取得下一个element对象,并将其作为输入参数带入到递归方法中。

这样,递归方法每次执行体中只针对当下$phrase-throughs[1]这个对象做业务(这里是database-add-phrase-through),通过递归带入下一个element进行处理。

实际上,这个递归的处理逻辑非常想C#中的foreach


另外,marklogic有一个标准的typeswith模式来做迭代的,你可以在官方snippet.xqy中看到源代码。


原创粉丝点击