问题
Hi how can i retrieve the URI of all the documents in directory. I create the below xquery to achieve that but that doesn't helps.
for $i in xdmp:directory("/Test/performance/results/","infinity")
let $k := document-uri(fn:doc($i))
return <li>{$k}</li>
回答1:
For efficiency you should use the URI lexicon.
cts:uris((), (), cts:directory-query("/Test/performance/results/","infinity"))
See https://docs.marklogic.com/cts:uris for documentation.
回答2:
How about:
xdmp:directory("/Test/performance/results/","infinity") !
<li>{fn:document-uri(.)}</li>
回答3:
The posters XQuery is incorrect because $i is already a document not a URI so running fn:doc($i) is not going to work.
Adam's solution will work (to infinate depth) but can be very slow as it requires fetching every document. On large databases thats very slow.
If you enable the URI Lexicon then you can do much better. Here is the source for the xmlsh marklogic "ls" command ... embedded in it is fairly simple XQuery.
https://github.com/DALDEI/xmlsh/blob/master/extensions/marklogic/src/org/xmlsh/marklogic/ls.xsh
The relevent code:
xquery version "1.0-ml";
declare variable $root external;
for $p in fn:distinct-values(
for $d in cts:uris($root,"document",
if( $root eq "" ) then () else cts:directory-query($root,"infinity"))
let $p := substring-after( $d , $root )
where $d ne $root
return
if( contains($p,"/") ) then fn:concat($root , substring-before( $p , "/" ) , "/" )
else
concat($root ,$p)
)
where $p eq "" or $p ne $root
return $p
This lists only the direct children of a directory using the uri lexicon
This program is a bit more complex - it tries to use the uri lexicon but if it fails reverts to the xdmp:directory ... it lists either direct or recursive depending on the -r flag
https://github.com/DALDEI/xmlsh/blob/master/extensions/marklogic/src/org/xmlsh/marklogic/list.xsh
来源:https://stackoverflow.com/questions/26139652/how-to-list-all-the-uri-of-all-documents-in-a-directory-in-marklogic