6
我想使用XSLT將XML轉換爲JSON。以下是我的XML和XSLT代碼。使用XSLT將XML轉換爲JSON的問題
XML文件:
<?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文件:
<?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>
XSLT的輸出:
{
"catalog":[
{
"title":"Empire Burlesque",
"artist":"Bob Dylan"
},
{
"title":"Subrayana kathe",
"artist":"Subba"
},(Problematic comma)
]
}
的問題是,有一個額外的逗號( '')在數組中最後一個對象的結尾。有沒有辦法在XSLT中避免這種情況?