【问题标题】:Using XSLT to create JSON output single quote conversion (XML to JSON)使用 XSLT 创建 JSON 输出单引号转换(XML 到 JSON)
【发布时间】:2016-12-15 09:43:47
【问题描述】:

当我将输入 XML 文件转换为 JSON 输出时,单引号属性被转换为双引号。

请任何人指导我解决上述问题。

我的输入 XML 文件是:

<items>
<mlu1_item>
<title>
<strong>Creatinine</strong>
</title>
<content>
<p>Creatinine is a normal waste product</p>
<ul>
<li>males</li>
<li>females</li>
</ul>
<p>If your creatinine level kidneys.</p>
</content>
<mlu1_button/>
<button_class/>
<link/>
<image>icon.png</image>
</mlu1_item>
</items>

我用过的XSL:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://json.org/" exclude-result-prefixes="#all">

<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />

<xsl:template match="my-node">
<xsl:value-of select="json:generate(.)"/>
</xsl:template>

<xsl:template match="items">
items: 
[
<xsl:for-each select="mlu1_item">
{
"title": "<xsl:value-of select="title/strong"/>"
"content": "<h4><xsl:value-of select="title/strong"/></h4>**<div class='text-left'>**<xsl:apply-templates select="content"/></div>",
"link": "",
"image": "img/<xsl:value-of select="image"/>",
"top": "5%",
"left": "52%",
"size": "",
"color": "",
"borderColor": "#00",
"bgInfoColor": "#fff",
"borderWidth": "0px",
},
</xsl:for-each>
]
</xsl:template>

<xsl:template match="p">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="ol">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="ul">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="li">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="content">
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

我得到的输出 JSON:

items: 
[
{
"title": "Creatinine"
"content": "<h4>Creatinine</h4>
**<div class="text-left">**
<p>Creatinine is a normal waste product</p>
<ul>
<li>males</li>
<li>females</li>
</ul>
<p>If your creatinine level kidneys.</p>
</div>",
"link": "",
"image": "img/icon.png",
"top": "5%",
"left": "52%",
"size": "",
"color": "",
"borderColor": "#00",
"bgInfoColor": "#fff",
"borderWidth": "0px",
},
]
};

但我希望输出为:

items: 
[
{
"title": "Creatinine"
"content": "<h4>Creatinine</h4>
**<div class='text-left'>**
<p>Creatinine is a normal waste product</p>
<ul>
<li>males</li>
<li>females</li>
</ul>
<p>If your creatinine level kidneys.</p>
</div>",
"link": "",
"image": "img/icon.png",
"top": "5%",
"left": "52%",
"size": "",
"color": "",
"borderColor": "#00",
"bgInfoColor": "#fff",
"borderWidth": "0px",
},
]
};

我想在 JSON 输出中为 div 属性声明保留单引号

【问题讨论】:

  • 您使用哪种 XSLT 处理器?最近发布的 Saxon 或 Altova 支持 XPath 函数来序列化 XML 和创建 JSON,因此它们在创建格式正确、平衡良好或转义的引号方面可能比某些模板做得更好。
  • 我用于转换的 Saxon-PE 9.6.0.7 版本
  • 你在 oXygen 中使用 Saxon 吗?至于输出,应该是 json.org 上指定的 JSON 格式吗?
  • 是的,在 oXygen 中使用 Saxon

标签: json xml xslt xslt-2.0


【解决方案1】:

您真的不想使用 XML 输出方法来创建不是 XML 的东西。 XML 输出方法不允许您控制是否在属性值周围使用单引号或双引号,因为它假定您正在编写 XML,并且在 XML 中没有区别。

如果您想要 XML 以外的其他格式的 XML 片段,这正是 XPath 3.0/3.1 中的 fn:serialize() 函数的用途。

您可以使用它来创建 XML 片段,然后将其合并到 JSON 结构中,然后您可以使用 JSON 输出方法对其进行序列化; JSON 输出方法会将字符串内容中的任何双引号转义为\"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 2020-01-26
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2019-12-28
    相关资源
    最近更新 更多