Skip to content

Latest commit

 

History

History

ConvertJsonToXMLusingXSLT30

Convert JSON to XML using XSLT Mappings

| Recipes by Topic | Recipes by Author | Request Enhancement | Report a bug | Fix documentation |

Kamlesh Zanje Kamlesh Zanje

This recipe converts and incoming file in JSON format into XML format

Download the integration flow sample

Recipe

Step Code Why?
Declare namespace for xpath functions Namespace xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xs="http://www.w3.org/2001/XMLSchema" Import of the function libraries in the XSLT
Function:json-to-xml usage usage Parses a string supplied in the form of a JSON text, returning the results in theformof an XML document node

References

Sample integration flow

The integration flow depicted in the recipe is very simple which contains start timer, content modifier to feed the payload and XSLT Mapping step through which we can leverage the benefit of XSL 3.0 specification that shall convert the JSON payload to XML.

iflowimage

Sample Script

This is the script used in the sample integration flow which starts with namespace declaration and then uses the templates to convert the json to xml.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:math="http://www.w3.org/2005/xpath-functions/math"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs math"
      version="3.0">

  <xsl:mode
      streamable="yes" />

  <xsl:output
      indent="yes" />

  <xsl:template
      match="data">
    <xsl:copy>
      <xsl:apply-templates
            select="json-to-xml(.)/*"/>    
    </xsl:copy>
  </xsl:template>

  <xsl:template
        match="*[@key]"
        xpath-default-namespace="http://www.w3.org/2005/xpath-functions">
      <xsl:element name="{@key}">
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Sample Input

The JSON provided in this image should be feed in the message body of the content modifier - "JSON Payload". Input file

Sample Output

Output of the JSON to XML conversion can be experienced in the Monitor's message processing view. Output Image