Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems converting XML to JSON using XSLT

Tags:

json

xml

xslt

I am trying to convert an XML to JSON using XSLT. Following are my XML and XSLT code.

XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Subrayana kathe</title>
        <artist>Subba</artist>
        <country>India</country>
        <price>30</price>
        <year>1986</year>
    </cd>
</catalog>

XSLT file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      {
"catalog":[
      <xsl:for-each select="catalog/cd">
         {"title":"
         <xsl:value-of select="title" />
         ",
"artist":"
         <xsl:value-of select="artist" />
         "},
      </xsl:for-each>
      ]
      }
   </xsl:template>
</xsl:stylesheet>

Output of XSLT:

{
   "catalog":[
      {
         "title":"Empire Burlesque",
         "artist":"Bob Dylan"
      },
      {
         "title":"Subrayana kathe",
         "artist":"Subba"
      },(Problematic comma)
   ]
}

The problem is that there is an extra comma(',') at the end of the last object in the array. Is there a way to avoid that in XSLT ?

like image 541
CCoder Avatar asked Dec 18 '13 12:12

CCoder


People also ask

Can XSLT transform XML to JSON?

JSON is a lightweight data-interchange format based on a subset of the JavaScript language, and often offered as an alternative to XML in—for example—web services. To make life easier XSLTJSON allows you to transform XML to JSON automatically.

Is there any benefit of converting XML to XSLT?

XSLT is commonly used to convert XML to HTML, but can also be used to transform XML documents that comply with one XML schema into documents that comply with another schema. XSLT can also be used to convert XML data into unrelated formats, like comma-delimited text or formatting languages such as troff.

Can XSLT work with JSON?

Transform JSON using XSLT A few predefined document templates are even available to help you define the XSLT necessary for the processing. Once the XSLT file is created, you just need to run a transformation over your stylesheet and reference your JSON document within the transformation scenario.

Is XSLT 2.0 backward compatibility?

The XSLT 2.0 engine is backwards compatible. The only time the backwards compatibility of the XSLT 2.0 engine comes into effect is when using the XSLT 2.0 engine to process an XSLT 1.0 stylesheet.


1 Answers

Only write the comma if there is another cd element in your xml.

So basically you have to wrap that comma in a xsl:if statement like this: <xsl:if test="./following-sibling::cd">,</xsl:if>

So your stylesheet will look something like that:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      {
"catalog":[
      <xsl:for-each select="catalog/cd">
         {"title":"
         <xsl:value-of select="title" />
         ",
"artist":"
         <xsl:value-of select="artist" />
         "}<xsl:if test="./following-sibling::cd">,</xsl:if>
      </xsl:for-each>
      ]
      }
   </xsl:template>
</xsl:stylesheet>
like image 136
Matthias Avatar answered Nov 03 '22 18:11

Matthias