Search code examples
marklogic

Setting collection name dynamically based on original directory structure


I would like to dyamically add original directory name as collection when loading files.

Suppose having following file: /home/sampledata/dir1/targetfile.xml I would like targetfile.xml included in following collections: "home", "sampledata", "dir1"

Can we do this while importing via MLCP? or will be appreciate if anyone can share alternative way to achieve by script.


Solution

  • You can use this query:

      let $dir-seprator := "/"
      for $uri in cts:uri-match("*")[fn:ends-with(.,'.xml')][1 to 10]
      let $collection := fn:substring-before($uri,fn:tokenize($uri,$dir-seprator)[fn:last()])
      return 
      xdmp:document-set-collections($uri,fn:tokenize($collection,$dir-seprator)[.!='']))