2017-07-17 76 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) 
相关问题