Could you please provide XSLT code to convert from XML to JSON? I am SAP developer with zero
knowledge in XSLT. The XSLT must be able to convert the XML to JSON format as below.I need to convert an XML source to a specified JSON format. To do this i need to remove header nodes, retain the array body and encapsulate in [ ]. I have converted the body but i am having trouble removing the header nodes and inserting the encapsulating [ ]
This is the XML format I receive:
<?xml version="1.0" encoding="utf-8"?>
<n0:AA_JSON_Out xmlns:n0="http://PELASEHELP.com/PI/YYY" "namespace
xmlns:prx="urn:sap.com:proxy:AAA:/1SAI/TAS3DF8912DDF823A9E7198:750">
<root>
<header>
<id>GGG-00009</id>
</header>
<body>
<user_name>RAMBO</user_name>
<code>BBB</code>
<date>20190405</date>
<time>015553</time>
<timezone>GMT</timezone>
<mail>52170302634</mail>
</body>
</root>
<root>
<header>
<id>GGG-00009</id>
</header>
<body>
<user_name>HULK</user_name>
<code>UUU</code>
<date>20190405</date>
<time>015553</time>
<timezone>GMT</timezone>
</body>
</root>
</n0:AA_JSON_Out>
This is what need to produce:
[{"header":{},"body":{}},{"header":{},"body":{}}]
Below is the output code `enter code here`result.
[
{
"header":{
"id":"GGG-00009"
},
"body":{
"user_name":"RAMBO",
"code":"BBB",
"date":"20190405",
"time":"015553",
"timezone":"GMT",
"identfier":"52170302317"
}
},
{
"header":{
"id":"GGG-00009"
},
"body":{
"user_name":"HULK",
"code":"UUU",
"date":"20190405",
"time":"015553",
"timezone":"GMT"
}
}
]
XSLT Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">[<xsl:apply-templates select="@* | node()"/>]</xsl:template>
<!-- Object or Element Property-->
<xsl:template match="*">
<xsl:variable name="childName" select="name()" /><xsl:if test="not(contains($childName, 'root')) and not(contains($childName, 'n0'))">"<xsl:value-of select="normalize-space(name())"/>":</xsl:if><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()"/>
<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:if test="not(contains($childName, 'root'))">{</xsl:if>
<xsl:apply-templates select="@*"/><xsl:apply-templates select="*"/><xsl:if test="not(contains($childName, 'root'))">}</xsl:if></xsl:otherwise>
</xsl:choose>
<xsl:if test="(contains($childName, 'body'))">}</xsl:if>
<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>
Please help me to provide XSLT code.
Thank you,
S,Saravannan