I have a requirement to convert xml to json. few elements required an array and for few elements it is not required.The Initial root tag also needs to be remove. Below is my 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>
For the above xml i have used the below XSLT code to convert into 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>
I am expecting below output, but with above code it is not giving the correct output.
[
{
"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
}
]
Please note that if the row is single also it should show the array symbol. locks and infostore nodes also should be always array even if it single in row.All fields should be in string and standAloneFlag field should be with out quotes.
Could you please help me what corrections i have do to for the correct output.