Problems converting XML to JSON using XSLT

前端 未结 1 540
小鲜肉
小鲜肉 2021-01-05 12:53

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

XML file:

         


        
相关标签:
1条回答
  • 2021-01-05 13:09

    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>
    
    0 讨论(0)
提交回复
热议问题