我有以下 xml:
<root>
"countryList":
[
{
"countryName": "UK",
"countryId": "909",
"countryCapital": "london"
},
{
"countryName": "India",
"countryId": "910",
"countryCapital": "Delhi"
}
]
</root>
我需要迭代这个数组以获得以下 json 输出:
{
"countries":
[
{
"Name": "UK",
"Id": "909",
"Capital": "london"
},
{
"Name": "India",
"Id": "910",
"Capital": "Delhi"
}
]
}
有人可以帮助我实现同样的目标吗?赞赏!
到目前为止我尝试过的是:
<xsl:text>{ countries: </xsl:text>
<xsl: for-each = ="substring-before(substring-after(., '"countryList": '), ',')" >
<xsl:text>{ Name: </xsl:text>
<xsl:value-of select="substring-before(substring-after(., '"countryName": '), ',')"/>
<xsl:text>, Id: </xsl:text>
<xsl:value-of select="substring-before(substring-after(., '"countryId": '), ',')"/>
<xsl:text>} </xsl:text>
</xsl: for-each>
这应该可行 - 我在代码中添加了注释
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- copy everything as is template - note the { and } around the copy -->
<xsl:template match="node() | @*">
{<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>}
</xsl:template>
<!-- template for the root element -->
<xsl:template match="/*">
<xsl:apply-templates select="node()" />
</xsl:template>
</xsl:stylesheet>