我需要将 xml 转换为 json。少数元素需要数组,对于少数元素则不需要。初始根标记也需要删除。下面是我的xml。
<?xml version="1.0" encoding="UTF-8" ?>
<ns1:Account_Resp xmlns:ns1="test:test:services">
<row>
<bank>
<bank>1234</bank>
<bankShortName>customer</bankShortName>
</bank>
<card>
<cardLastFour>000</cardLastFour>
<cardType>00n</cardType>
</card>
<address>
<city>CA</city>
<country>CA</country>
</address>
<version>
<symbol>20200702111359</symbol>
</version>
<locks>
<lockType>06</lockType>
<lockValidFrom>00</lockValidFrom>
<lockValidTo>9</lockValidTo>
</locks>
<locks>
<lockType>0</lockType>
<lockValidFrom>000</lockValidFrom>
<lockValidTo>99</lockValidTo>
</locks>
<infoStore>
<informationCategoryCode></informationCategoryCode>
<infoDateFrom></infoDateFrom>
</infoStore>
<changedTime>111359</changedTime>
<standAloneFlag>false</standAloneFlag>
</row>
<row>
<bank>
<bank>1234</bank>
<bankShortName>customer</bankShortName>
</bank>
<card>
<cardLastFour>000</cardLastFour>
<cardType>00n</cardType>
</card>
<address>
<city>CA</city>
<country>CA</country>
</address>
<version>
<symbol>20200702111359</symbol>
</version>
<locks>
<lockType>06</lockType>
<lockValidFrom>00</lockValidFrom>
<lockValidTo>9</lockValidTo>
</locks>
<locks>
<lockType>0</lockType>
<lockValidFrom>000</lockValidFrom>
<lockValidTo>99</lockValidTo>
</locks>
<infoStore>
<informationCategoryCode></informationCategoryCode>
<infoDateFrom></infoDateFrom>
</infoStore>
<changedTime>111359</changedTime>
<standAloneFlag>false</standAloneFlag>
</row>
</ns1:Account_Resp>
对于上面的 xml,我使用下面的 XSLT 代码转换为 json。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://use your namespace">
<xsl:output method="text"/>
<xsl:template match="/ns0:Account_Resp">{
<xsl:apply-templates select="*"/> }
</xsl:template>
<!-- Object or Element Property-->
<xsl:template match="*">
"<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/>
</xsl:template>
<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="Properties"/>
</xsl:template>
<!-- Object Properties -->
<xsl:template name="Properties">
<xsl:variable name="childName" select="name(*[1])"/>
<xsl:choose>
<xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when>
<xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when>
<xsl:otherwise>{
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="*"/>
}</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<!-- Attribute Property -->
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
</xsl:template>
</xsl:stylesheet>
我期待低于输出,但使用上面的代码它没有给出正确的输出。
[
{
"bank": {
"bank": "1234",
"bankShortName": "customer"
},
"card": {
"cardLastFour": "000",
"cardType": "00n"
},
"address": {
"city": "Monicashire",
"country": "CA"
},
"version": {
"symbol": "2345"
},
"locks": [
{
"lockType": "06",
"lockValidFrom": "00",
"lockValidTo": "9"
},
{
"lockType": "06",
"lockValidFrom": "000",
"lockValidTo": "999"
}
],
"infoStore": {
"informationCategoryCode": "",
"infoDateFrom": "",
"infoDateTo": ""
},
"changedTime": "111359",
"standAloneFlag": "false"
},
{
"bank": {
"bank": "1234",
"bankShortName": "customer"
},
"card": {
"cardLastFour": "000",
"cardType": "00n",
},
"address": {
"city": "ca",
"country": "CA",
},
"version": {
"symbol": "156"
},
"locks": [
{
"lockType": "06",
"lockValidFrom": "00010101",
"lockValidTo": "99991231"
},
{
"lockType": "06",
"lockValidFrom": "00010101",
"lockValidTo": "99991231"
}
],
"infoStore":[ {
"informationCategoryCode": "",
"infoDateFrom": "",
"infoDateTo": ""
}
],
"changedTime": "111359",
"standAloneFlag": false
}
]
请注意,如果行是单行也应该显示数组符号。锁和信息存储节点也应该始终是数组,即使它是单行的。所有字段都应该是字符串,standAloneFlag 字段应该不带引号。
您能否帮助我对正确的输出进行哪些更正。