将 xml 转换为 csv 时如何转义“,”逗号

How to escape "," comma when convert xml to csv

我有一个示例代码可以将 xml 转换为 csv,但是当我在 excel 中打开一个 csv 文件时。我看到三列,因为抽象元素有逗号“,”。我怎样才能修复它以逃避逗号“,”。谢谢

xquery version "1.0-ml";
declare namespace mdr="http://learning-convert-xml-csv.com/record";
declare namespace meta="http://learning-convert-xml-csv.com/meta";

let $doc := <records xmlns:record="http://learning-convert-xml-csv.com/record">
           <meta:Metadata xmlns:meta="http://learning-convert-xml- csv.com/meta">
           <meta:Title>Beginning MarkLogic with XQuery.</meta:Title>
           <meta:Abstract>In software engineering and computer science, abstraction is a technique for arranging complexity of computer systems          </meta:Abstract>

let $Title := ($doc/meta:Metadata/meta:Title)
let $Title := fn:string-join($Title,",")

let $abstract:=      ($doc/meta:Metadata/meta:Abstract)
let $abstract:=      fn:string-join($abstract,",")

let $quote:=
for $myData in ($Title,$abstract)
  return fn:concat("'",$myData,"'")

let $output :=
         text{
               fn:concat(fn:string-join(($quote),","),"&#x0D;&#x0A;"
               )
         }

 return xdmp:save(fn:concat("c:\temp\","test.csv"),$output)

使用双引号而不是单引号来引用 CSV 中的值。使用它你不需要转义除了双引号本身之外的任何其他字符,你可以通过将它加倍来完成。只要使用双引号,好的 CSV 解析器甚至应该能够在值内解析 line-ends。

另请参阅 RFC 4180:https://www.rfc-editor.org/rfc/rfc4180

HTH!