如何使用 xdmp:save 将 MarkLogic 数据保存在 csv 文件中

How to save MarkLogic data in csv file using xdmp:save

我想生成csv文件。下面的代码创建了 csv 文件,但它无法在每一行中附加数据行。

我错过了什么吗?

代码如下:

xquery version "1.0-ml";
let $filename := "D:\csv-reports\test.csv"

let $directory-query := cts:directory-query("/searcdoc/")
let $uris := cts:uris((), (), $directory-query)

for $uri in $uris
let $additional := doc($uri)/searcdoc/access/additional/text()
let $des := doc($uri)/searcdoc/std/text()
let $option := <options xmlns="xdmp:save"><method>text</method></options>
let $results := text{((fn:string-join(($uri, $des, $additional), ",")), "&#10;")}

return

xdmp:save($filename, $results, $option)

我认为您只需要稍微重新安排一下代码即可。试试这个:

xquery version "1.0-ml";
let $filename := "D:\csv-reports\test.csv"

let $directory-query := cts:directory-query("/searcdoc/")
let $option := <options xmlns="xdmp:save"><method>text</method></options>
let $uris := cts:uris((), (), $directory-query)
let $lines := 
  for $uri in $uris
  let $additional := doc($uri)/searcdoc/access/additional/text()
  let $des := doc($uri)/searcdoc/std/text()
  return fn:string-join(($uri, $des, $additional), ",")

return
  xdmp:save($filename, text{ fn:string-join($lines, "&#10;") }, $option)

编辑以将 "\n" 更改为 "&#10;"