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

时间:2017-02-14 05:47:46

标签: marklogic

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

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)

1 个答案:

答案 0 :(得分:1)

使用双引号而不是单引号来引用CSV中的值。使用它你不需要逃避任何其他角色,除了双引号本身,你通过加倍它。只要使用双引号,好的CSV解析器甚至应该能够在值内解析行尾。

另请参阅RFC 4180:https://tools.ietf.org/html/rfc4180

HTH!