2017-07-17 75 views
0

鑑於以下XQuery代碼生成路徑並基於MarkLogic數據庫中的數據保存文件,如何在不存在的情況下創建文件系統文件夾沒有得到例外?如何在保存文件時在文件系統上創建文件夾

for $doc in collection("http://example.com/stuff") 
let $folderName := name($doc/Envelope/*[1]) 
let $folderPath := concat("c:\temp\", $folderName, "\") 
let $fileName := concat($doc/Envelope/*[1]/*:Code/text(), ".xml") 
let $fullPath := concat($folderPath, $fileName) 

(: Create the folder at $folderPath if it does not exist :) 

return xdmp:save($fullPath,$doc) 

回答

4

AFAIK沒有「文件夾存在」功能,容易混淆filesystem-file-exists實際工作的文件夾了,所以答案應該是:

for $doc in collection("http://example.com/stuff") 
let $folderName := name($doc/Envelope/*[1]) 
let $folderPath := concat("c:\temp\", $folderName, "\") 
let $fileName := concat($doc/Envelope/*[1]/*:Code/text(), ".xml") 
let $fullPath := concat($folderPath, $fileName) 

let $_ := if(xdmp:filesystem-file-exists($folderPath)) then() else xdmp:filesystem-directory-create($folder) 

return xdmp:save($fullPath,$doc) 
相關問題