保存文件时如果文件夹不存在如何在文件系统上创建文件夹

How to create a folder on the file system if it does not exist when saving files

鉴于以下 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)

AFAIK 没有 "folder exists" 函数,令人困惑的是 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)