2

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

4
  • Which XSLT processor do you use? Recent releases of Saxon or Altova have support for XPath function to serialize XML and to create JSON so they might do a better job than some templates to create properly formatted and well-balanced or escaped quotes. Commented Dec 15, 2016 at 11:55
  • Saxon-PE 9.6.0.7 Version I have used for conversion Commented Dec 15, 2016 at 12:09
  • Do you use Saxon inside oXygen? As for the output, is that supposed to JSON as specified on json.org? Commented Dec 15, 2016 at 12:29
  • Yes using Saxon inside oXygen Commented Dec 15, 2016 at 12:39

1 Answer 1

2

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 \".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.