纯 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/title,
$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 要求任何文本中的逗号都用引号括起来,并且(之后)任何引号都被两个引号替换,最后,如果整个结果包含一个引号,它必须用 (单)引号。
下面是一个纯粹的 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)
当此 XPath 表达式针对此(使用新的测试用例扩展)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>
<book isbn="XX1234567">
<title>Quotes and comma</title>
<description>
Hello, World from "Ms-Excel"
</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.
"XX1234567,Quotes and comma,Hello"","" World from ""Ms-Excel"""