Issue transforming XML to JSON using XSLT, JSON output not as expected
I am using an XSLT mediator to transform an XML payload into a JSON payload, but the resulting JSON output is not as expected. Also, I just need to use this XSLT mediator, not any others. Here is my XSLT code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:ns="http://service.com">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
{
"response": {
"animals": {
"animal": [
<xsl:for-each select="//ns:animal">
{
"name": "<xsl:value-of select="ns:name"/>",
"color": "<xsl:value-of select="ns:color"/>",
"info": {
"animalInfo": [
<xsl:for-each select="ns:info/ns:animalInfo">
{
"someVal1": "<xsl:value-of select="ns:someVal1"/>",
"someVal2": "<xsl:value-of select="ns:someVal2"/>"
}<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
]
}
}<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
]
}
}
}
</xsl:template>
</xsl:stylesheet>
And here is the resulting JSON output as a string inside a “text” field:
{
"text":{
"response":{
"animals":{
"animal":[
{
"name": "name",
"color": "color",
"info":{
"animalInfo":[
{
"someVal1":"someVal1",
"someVal2":"someVal2"
},
{
"someVal1":"someVal3",
"someVal2":"someVal4"
}
]
}
},
{
"name": "name",
"color": "color",
"info":{
"animalInfo":[
]
}
}
]
}
}
}
}
I tried so hard to fix this issue but I am unable to resolve it and even not sure why this is keep happening. Can someone please help me to resolve this error? Thanks in advance.
Read more here: Source link
