Recursion 如何在XQuery中递归列出集合/资源

Recursion 如何在XQuery中递归列出集合/资源,recursion,xquery,exist-db,Recursion,Xquery,Exist Db,我希望以递归方式列出特定点的所有集合: declare function local:list-collections($collection as xs:string) { for $child in xmldb:get-child-collections($collection) return local:list-collections(concat($collection, '/', $child)) }; local:list-collections(

我希望以递归方式列出特定点的所有集合:

declare function local:list-collections($collection as xs:string) {
    for $child in xmldb:get-child-collections($collection)
    return
        local:list-collections(concat($collection, '/', $child))
};

local:list-collections('/db/apps/tested-bunny/data/')

这不会返回任何内容(没有错误,没有结果)。我受到启发并认为它是递归设置权限的好起点,等等。

< P>参见<代码> dBUTIL:SCAN—*()/<代码>在Wolfgang Meier的文章中关于存在的DB 2 +。总的来说,这篇文章很有启发性。现在,
dbutil
模块在默认情况下使用eXist安装的
共享资源
包中可用,因此您可以按如下方式使用它:

xquery version "3.0";

import module namespace dbutil="http://exist-db.org/xquery/dbutil" 
    at "/db/apps/shared-resources/content/dbutils.xql";

dbutil:scan-collections(
    xs:anyURI('/db'), 
    function($collection) { $collection } 
)

这些功能表现良好。我刚刚在eXide中运行了这个,查询在0.699秒内返回了4125个集合名称。

您的查询实际上是递归查找集合,但没有输出。我建议做一些类似的事情

declare function local:list-collections($collection as xs:string) {

    for $child in xmldb:get-child-collections($collection)
    let $childCollection := concat($collection, '/', $child)
    return
        (local:list-collections($childCollection), $childCollection)
};

local:list-collections('/db/apps/fundocs')

但乔的建议肯定更简洁。

谢谢你的回答,我忘了。不过,在xquery中,HoF的使用非常先进,它可能有点太复杂了。谢谢,实际上,我喜欢这两种解决方案!乔的那个可能更优雅,但我需要了解里面发生了什么。这一个基于更简单的逻辑,对我来说很清楚。只需注意:我不知道为什么,但似乎这个函数实际上并没有列出树中的所有内容。它似乎只返回一些更深的分支。在eXide中很难完全测试(它只允许我返回10项)。实际上,这一项返回14项,其他31项(这似乎是真的)。这个有点小毛病。如果有子集合,它似乎会停止列出当前父集合。有趣的是,此函数返回的项总是比扫描项少1项:)我不知道是哪一项,猜测它可能是根集合。