2
将输入XML文件转换为JSON输出时,单引号属性转换为双引号。使用XSLT创建JSON输出单引号转换(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输出
您使用哪种XSLT处理器?最近Saxon或Altova的发布版本支持XPath函数来序列化XML并创建JSON,因此它们可能会比一些模板做得更好,以创建格式正确,均衡或转义的引号。 –
Saxon-PE 9.6.0.7我用于转换的版本 –
您是否在oXygen中使用Saxon?至于输出,是否应该在json.org上指定JSON? –