使用XQuery进行XML到CSV的转换

前端之家收集整理的这篇文章主要介绍了使用XQuery进行XML到CSV的转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 XML文件,我需要将其转换为XQuery.考虑一组简单的XML:

books[book]
book[@isbn,title,descrption]

例如:

<books>
    <book isbn="1590593049">
        <title>Extending Flash MX 2004</title>
        <description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
    </book>
    <book isbn="0132149184">
        <title>Java Software Solutions</title>
        <description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
    </book>

如何使用XQuery将其转换为CSV格式? CSV由Microsoft Excel使用,

所以它将用逗号(,)字符分隔,特殊字符应该被转义.

解决方法

一个纯XPath 2.0表达式:

for $b in /*/book
    return
      concat(escape-html-uri(string-join(($b/@isbn,$b/title,$b/description
                                          )
                                           /normalize-space(),",")
                             ),codepoints-to-string(10))

基于XSLT 2的验证:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "for $b in /*/book
       return
         concat(escape-html-uri(string-join(($b/@isbn,$b/description
                                             )
                                              /normalize-space(),',')
                                ),codepoints-to-string(10))"/>
 </xsl:template>
</xsl:stylesheet>

当对提供的XML文档应用此转换时(根据其格式不正确):

<books>
    <book isbn="1590593049">
        <title>Extending Flash MX 2004</title>
        <description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
    </book>
    <book isbn="0132149184">
        <title>Java Software Solutions</title>
        <description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
    </book>
</books>

产生了想要的正确结果:

1590593049,Extending Flash MX 2004,Using javascript alongwith actionscript 3.0 and mxml.
 0132149184,Java Software Solutions,Complete book full of case studies on business solutions and design concepts while building mission critical business applications.

更新:

评论中,OP已经请求任何文本逗号被引号包围,并且(之后)任何引号被两个引号替换,并且最后,如果wholw结果包含引号,则它必须被(包围)(单引号.

这是一个纯XPath 2.0表达式,它产生了这个:

for $b in /*/book,$q in codepoints-to-string(34),$NL in codepoints-to-string(10),$isbn in normalize-space(replace($b/@isbn,concat($q,$q))),$t in normalize-space(replace($b/title,$d in normalize-space(replace($b/description,$res in
     escape-html-uri(string-join(($isbn,$t,$d),')),$res2 in replace($res,$q,$q))
   return
    if(contains($res2,$q))
       then concat($q,$res2,$NL)
       else concat($res2,$NL)

当针对此(使用新的测试用例扩展)XML文档评估此XPath表达式时:

<books>
    <book isbn="1590593049">
        <title>Extending Flash MX 2004</title>
        <description>
        Using javascript alongwith actionscript 3.0 and mxml.</description>
    </book>
    <book isbn="0132149184">
        <title>Java Software Solutions</title>
        <description>
            Complete book full of case studies on business solutions and design concepts while building mission critical
            business applications.
        </description>
    </book>
    <book isbn="XX1234567">
        <title>Quotes and comma</title>
        <description>
            Hello,World from "Ms-Excel"
        </description>
    </book>
</books>

产生了想要的正确结果:

1590593049,Using javascript alongwith actionscript 3.0 and mxml.
0132149184,Complete book full of case studies on business solutions and design concepts while building mission critical business applications.
"XX1234567,Quotes and comma,Hello"","" World from ""Ms-Excel"""

猜你在找的XML相关文章