we are trying to do xml to json conversion. child elements 'EmployeeCode' is not recognizing. Error:Error executing XSLT at line 15 : xml-to-json: Child elements of must have a key attribute. Need to consider the child elements and subelements in xml to json conversion.
xmlFile:
<root>
<Name>Alex</Name>
<Age>42</Age>
<item>
<number>314</number>
<name>foo</name>
<EmployeeCode>
<id>F21</id>
<Department>
<Designation>Engineer</Designation>
</Department>
<Status>Contractor</Status>
</EmployeeCode>
</item>
<item>
<number>42</number>
<name>bar</name>
<EmployeeCode>
<id>F24</id>
<Department>
<Designation>Manager</Designation>
</Department>
<Status>Full-time</Status>
</EmployeeCode>
</item>
<data>test</data>
</root>
xslcode:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
xmlns="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="json-xml">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="xml-to-json($json-xml, map { 'indent' : true() })"/>
</xsl:template>
<xsl:template match="*[not(*)]">
<string key="{local-name()}">{.}</string>
</xsl:template>
<xsl:template match="*[not(*) and . castable as xs:double]">
<number key="{local-name()}">{.}</number>
</xsl:template>
<xsl:template match="*[*]">
<map>
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:choose>
<xsl:when test="current-group()[2]">
<array key="{local-name()}">
<xsl:apply-templates select="current-group()"/>
</array>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</map>
</xsl:template>
<xsl:template match="node()|@*">
<!-- Including any attributes it has and any child nodes -->
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
Expected Json file:
{
"Name": "Alex",
"Age": 42,
"item": [
{
"number": 314,
"name": "foo",
"EmployeeCode": {
"id": "F21",
"Department": {
"Designation": "Engineer"
},
"Status": "Contractor"
}
},
{
"number": 42,
"name": "bar",
"EmployeeCode": {
"id": "F24",
"Department": {
"Designation": "Manager"
},
"Status": "Full-time"
}
}
],
"data": "test"
}