使用XSLT创建JSON输出单引号转换(XML到JSON) [英] Using XSLT to create JSON output single quote conversion (XML to JSON)

查看:51
本文介绍了使用XSLT创建JSON输出单引号转换(XML到JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While I'm converting input XML file to JSON output, single quote attributes are converted into double quotes.

请任何人指导我解决以上问题.

Please anyone guide me to resolve above issue.

我输入的XML文件是:

My input XML file is:

<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:

Output JSON which I got as:

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属性声明保留单引号

I want to remain single quote for div attributes declaration in JSON output

推荐答案

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

You really don't want to be using the XML output method to create something that isn't XML. The XML output method doesn't allow you to control whether single quotes or double quotes are used around attribute values, because it assumes you are writing XML, and in XML it makes no difference.

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

If you want fragments of XML within some other format that isn't XML, this is exactly what the fn:serialize() function in XPath 3.0/3.1 is for.

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

You can use this to create pieces of XML which you then incorporate in a JSON structure which you can then serialize using the JSON output method; the JSON output method will escape any double quotation marks in the content of a string as \".

这篇关于使用XSLT创建JSON输出单引号转换(XML到JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆