XSLT Mapping and JSON

When generating a payload in SAP CPI to use the BRM rule engines, I had to transform a payload into a json payload with all requested vocabulary. To Generate a mapping to JSON, use the following :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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"
	xmlns:ns0="http:/my.namespace.com"
	exclude-result-prefixes="#all"
	version="3.0">
  
  <xsl:output method="text" indent="yes"/> /* Required to output json payload as text */
  
  <xsl:param name="MyRuleId" select="MyBRMRuleId" /> /* Sample ID used to unittest my mapping */

  <xsl:template match="/ns0:content">
  {
  "RequestId": "<xsl:value-of select="$MyRuleId" />",
  "Vocabulary": [
      {
        "Entity": {
          "Field1" : "<xsl:value-of select="Source" />",
          "Field2" : "<xsl:value-of select="Target" />",
          "Field3" : "<xsl:value-of select="Priority" />",
          "Field4" : "<xsl:value-of select="Type" />"
        }
      }
    ]
  }
  </xsl:template>
  
</xsl:stylesheet>