Boopathy S Boopathy S - 7 days ago 5
JSON Question

The tagging element is not coming in JSON Output

I want the order list element come as like the input XML file in the output JSON file. I tried using XSLT, but it's not working

My input XML file is:

<description>
<p>This medicine is classified as a GLP-1 receptor agonist.</p>
<ol>
<li>Use this medicine once a week</li>
</ol>
</description>


The XSL Which I use is:

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

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

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

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


Output XML which I got is:

description: 'This medicine is classified as a b : "GLP-1 receptor agonist."'
Use this medicine once a week


I expect output JSON as:

description: 'This medicine is classified as a b : "GLP-1 receptor agonist."'
<ol><li>Use this medicine once a week</li></ol>


Is this possible in XML to JSON conversion?

Please check and provide me the correct XSLT code. Thanks in Advance.

Answer

I completed your XSLT to

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />
 <xsl:strip-space elements="*" />  

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

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

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

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

</xsl:stylesheet>

The trick - the difference between both of you outputs lies in the method attribute of the <xsl:output ...> element at the beginning.

If you use

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

you will get

description: 
This medicine is classified as a GLP-1 receptor agonist.Use this medicine once a weekctbankix@ctbankix:~/Do

and if you use

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

you will get

description: 
    This medicine is classified as a GLP-1 receptor agonist.<ol>
  <li>Use this medicine once a week</li>
</ol>

as desired.