| Recipes by Topic | Recipes by Author | Request Enhancement | Report a bug | Fix documentation |
Kamlesh Zanje |
---|
This recipe converts and incoming file in JSON format into XML format
Download the integration flow sample
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 |
- Saxonica - json-to-xml function
- SAP Help - Create XSLT Mapping
- Cloud Platform Integration – XSLT Mapping is enriched with XSLT 3.0 specification
- What is new in XSLT 3.0
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.
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>
The JSON provided in this image should be feed in the message body of the content modifier - "JSON Payload".
Output of the JSON to XML conversion can be experienced in the Monitor's message processing view.