如何避免XML在JSON中的solidus和双引号转义?
鉴于此,
/)可以(但不需要)在JSON中转义,'而不是"来避免在JSON字符串值中转义,在XSLT中实现这些潜在的序列化改进的最佳方法是什么?
这个XML
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<array key="o_array">
<map>
<string key="s/1">x/y/z</string>
</map>
<map>
<string key="s2"><![CDATA[<a href="/x/y">Link</a> a/b "test"]]></string>
</map>
</array>
</map>输入到此XSLT,
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="xml-to-json(.,map{'indent':true()})"/>
</xsl:template>
</xsl:stylesheet>产量(通过撒克逊,XSLT Fiddle演示)这个JSON输出:
{ "o_array" :
[
{ "s\/1" : "x\/y\/z" },
{ "s2" : "<a href=\"\/x\/y\">Link<\/a> a\/b \"test\"" } ] }为了美观(上面的JSON太难看了)和最小化文件大小(在禁用缩进之后),我想生成以下JSON:
{ "o_array" :
[
{ "s/1" : "x/y/z" },
{ "s2" : "<a href='/x/y'>Link</a> a/b \"test\"" } ] }备注:
saxon:single-quotes,似乎很有帮助,但我不清楚如何在xml-to-json()中使用这个选项。map{'method': 'json', 'use-character-maps': map{ '/': '/' }} as 马丁·霍宁,看起来很有帮助,但是,同样地,如何在xml-to-json()中使用这个选项会让我感到困惑。string/@escape和string/@escape-key属性的解读,并通过实验证实了这一点,这是无能为力的。发布于 2019-05-30 17:49:22
只有当您愿意引入parse-json() => serialize(...)步骤时,才能使用带有字符映射的链接建议:
. => xml-to-json() => parse-json() => serialize(map { 'method' : 'json', 'use-character-maps' : map { '/' : '/' } })那样的话
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select=". => xml-to-json() => parse-json() => serialize(map { 'method' : 'json', 'use-character-maps' : map { '/' : '/' } })"/>
</xsl:template>
</xsl:stylesheet>在https://xsltfiddle.liberty-development.net/b4GWVd/25我得到
{"o_array":[{"s/1":"x/y/z"},{"s2":"<a href=\"/x/y\">Link</a> a/b \"test\""}]}要在作为XML片段的字符串值上插入Saxon特定的序列化参数,我认为可以尝试首先通过简单地执行另一个解析和序列化步骤的模式运行输入,这一次只需如下所示
. => parse-xml-fragment() => serialize(map {
'method': 'xml',
QName('http://saxon.sf.net/', 'single-quotes'): true()
})在oXygen中使用Saxon9.9 EE和
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of
select="
$single-quotes => xml-to-json() => parse-json() => serialize(map {
'method': 'json',
'use-character-maps': map {'/': '/'}
})"
/>
</xsl:template>
<xsl:variable name="single-quotes">
<xsl:apply-templates mode="serialize-fragments"/>
</xsl:variable>
<xsl:mode name="serialize-fragments" on-no-match="shallow-copy"/>
<xsl:template match="string" mode="serialize-fragments"
xpath-default-namespace="http://www.w3.org/2005/xpath-functions">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:try
select="
. => parse-xml-fragment() => serialize(map {
'method': 'xml',
QName('http://saxon.sf.net/', 'single-quotes'): true()
})">
<xsl:catch select="string()"/>
</xsl:try>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>我得到了
{"o_array":[{"s/1":"x/y/z"},{"s2":"<a href='/x/y'>Link</a> a/b \"test\""}]}https://stackoverflow.com/questions/56379774
复制相似问题