recursiontransactionsxquerymarklogicxquery-update

Recursive copy of a folder with XQuery


I have to copy an entire project folder inside the MarkLogic server and instead of doing it manually I decided to do it with a recursive function, but is becoming the worst idea I have ever had. I'm having problems with the transactions and with the syntax but being new I don't find a true way to solve it. Here's my code, thank you for the help!

import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";

declare option xdmp:set-transaction-mode "update";

declare function local:recursive-copy($filesystem as xs:string, $uri as xs:string)
{
  for $e in xdmp:filesystem-directory($filesystem)/dir:entry
  return 
    if($e/dir:type/text() = "file")
        then dls:document-insert-and-manage($e/dir:filename, fn:false(), $e/dir:pathname)
    else
      (
          xdmp:directory-create(concat(concat($uri, data($e/dir:filename)), "/")),
          local:recursive-copy($e/dir:pathname, $uri)
      )

};

let $filesystemfolder := 'C:\Users\WB523152\Downloads\expath-ml-console-0.4.0\src'
let $uri := "/expath_console/"

return local:recursive-copy($filesystemfolder, $uri)

Solution

  • MLCP would have been nice to use. However, here is my version:

    declare option xdmp:set-transaction-mode "update";
    
    declare variable $prefix-replace := ('C:/', '/expath_console/');
    
    declare function local:recursive-copy($filesystem as xs:string){
       for $e in xdmp:filesystem-directory($filesystem)/dir:entry
        return 
          if($e/dir:type/text() = "file")
             then 
               let $source := $e/dir:pathname/text()
               let $dest := fn:replace($source, $prefix-replace[1], $prefix-replace[2]) 
               let $_ := xdmp:document-insert($source,
                  <options xmlns="xdmp:document-load">
                    <uri>{$dest}</uri>
                  </options>)
               return <record>
                         <from>{$source}</from>
                         <to>{$dest}</to>
                      </record>
             else
               local:recursive-copy($e/dir:pathname)
    
    };
    
    let $filesystemfolder := 'C:\Temp'
    
    return <results>{local:recursive-copy($filesystemfolder)}</results> 
    

    Please note the following: