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.
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 which I have used:
<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>
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",
},
]
};
But I expected output 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",
},
]
};
I want to remain single quote for div attributes declaration in JSON output
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.
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.
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
\"
.