I am trying to convert an XML to JSON using XSLT. Following are my XML and XSLT code.
XML file:
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>