Ran*_*rma 5 xml xquery altova marklogic xpath-2.0
我有一个XML文件,我需要将其转换为XQuery.考虑一组简单的XML:
books[book]
book[@isbn, title, descrption]
Run Code Online (Sandbox Code Playgroud)
例如:
<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>
Run Code Online (Sandbox Code Playgroud)
如何使用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))
Run Code Online (Sandbox Code Playgroud)
基于 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/title,
$b/description
)
/normalize-space(),
',')
),
codepoints-to-string(10))"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于提供的 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>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
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.
Run Code Online (Sandbox Code Playgroud)
更新:
在评论中,OP要求任何文本内逗号都用引号括起来,并且(之后)任何引号都用两个引号替换,最后,如果整个结果包含引号,则必须用 (单)引号。
下面是产生以下结果的纯 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, ',', concat($q,',',$q))),
$d in normalize-space(replace($b/description, ',', concat($q,',',$q))),
$res in
escape-html-uri(string-join(($isbn,$t,$d), ',')),
$res2 in replace($res, $q, concat($q,$q))
return
if(contains($res2, $q))
then concat($q, $res2, $q, $NL)
else concat($res2, $NL)
Run Code Online (Sandbox Code Playgroud)
当针对此(用新测试用例扩展的)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>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
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.
"XX1234567,Quotes and comma,Hello"","" World from ""Ms-Excel"""
Run Code Online (Sandbox Code Playgroud)