我有一个 xml,其中有一个 json,里面有一个数组。我正在尝试迭代该数组,但面临困难

问题描述 投票:0回答:1

我有以下 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(., '&quot;countryList&quot;: '), ',')" >
 <xsl:text>{ Name: </xsl:text>
    <xsl:value-of select="substring-before(substring-after(., '&quot;countryName&quot;: '), ',')"/>
<xsl:text>, Id: </xsl:text>
<xsl:value-of select="substring-before(substring-after(., '&quot;countryId&quot;: '), ',')"/>
<xsl:text>} </xsl:text>
</xsl: for-each>
    
xslt xslt-1.0 xsl-fo
1个回答
0
投票

这应该可行 - 我在代码中添加了注释

<?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>
© www.soinside.com 2019 - 2024. All rights reserved.