This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Sample 440: Converting JSON to XML Using XSLT

Objective: Demonstrate the ability to expose a SOAP service over JSON by switching between JSON and XML/SOAP message formats using the XSLT mediator and Enrich Mediator.

<definitions xmlns="http://ws.apache.org/ns/synapse"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">

    <proxy name="JSONProxy" transports="http https">
        <target>
            <endpoint>
                <address uri="http://localhost:9000/services/SimpleStockQuoteService" format="soap11"/>
            </endpoint>
            <inSequence>
                <log level="full"/>
                <xslt key="in_transform"/>
                <property name="messageType" scope="axis2" value="text/xml"/>
                <header name="Action" scope="default" value="urn:getQuote"/>
                <enrich>
                   <source xmlns:m0="http://services.samples" clone="true" xpath="//m0:getQuote"/>
                   <target type="body"/>
                </enrich>
            </inSequence>
            <outSequence>
                <log level="full"/>
                <xslt key="out_transform"/>
                <property name="messageType" scope="axis2" value="application/json"/>
                <send/>
            </outSequence>
        </target>
    </proxy>

    <localEntry key="in_transform">
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                        xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                        xmlns:m0="http://services.samples" version="2.0" exclude-result-prefixes="m0 fn">
            <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
            <xsl:template match="*">
                <xsl:element name="{local-name()}" namespace="http://services.samples">
                    <xsl:copy-of select="attribute::*"/>
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:template>
        </xsl:stylesheet>
    </localEntry>

    <localEntry key="out_transform">
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
            <xsl:output method="xml" version="1.0" encoding="UTF-8"/>
            <xsl:template match="*">
                <xsl:element name="{local-name()}">
                    <xsl:apply-templates/>
                </xsl:element>
            </xsl:template>
        </xsl:stylesheet>
    </localEntry>

</definitions>

1. Deploy the SimpleStockQuoteService in sample Axis2 server and start it on port 9000.

2. Start Synapse with the sample configuration 440 (i.e. wso2esb-samples -sn 440).

3. Setup Synapse and the sample Axis2 client for JSON (Refer Synapse Samples Setup Guide for details)

Use following command to invoke the proxy service:

ant jsonclient -Daddurl=http://localhost:8280/services/JSONProxy

jsonclient sends the following JSON request to the above proxy service:

{"getQuote":{"request":{"symbol":"WSO2"}}}

This request gets the following XML representation when it gets built in the ESB: (Take a look at the XML representation of JSON payloads within the ESB)

<jsonObject>
    <getQuote>
        <request>
            <symbol>WSO2</symbol>
        </request>
    </getQuote>
</jsonObject>

In the inSequence, the XSLT transformation in_transform transforms the above XML representation to the following XML by applying namespace declarations that are expected by the service.

<jsonObject xmlns="http://services.samples">
    <getQuote>
        <request>
            <symbol>WSO2</symbol>
        </request>
    </getQuote>
</jsonObject>

Finally the Enrich Mediator extracts the getQuote element from the above payload and attaches it as the first child of the current payload so that the final SOAP request that is sent to the service has the following format.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <getQuote xmlns="http://services.samples">
            <request>
                <symbol>WSO2</symbol>
            </request>
        </getQuote>
    </soapenv:Body>
</soapenv:Envelope>

We need to perform this sequence of transformations to the incoming JSON request to convert it and build the XML SOAP request expected by the service. Note that the XSLT transformation in the outSequence (line 22) can be omitted because the JSON message formatter already knows how to format any XML payload to JSON.

The final response will look like the following:

{
   "getQuoteResponse":{
      "return":{
         "change":3.853593376681722,
         "earnings":12.802850763714854,
         "high":67.92488310190126,
         "last":66.14619264746406,
         "lastTradeTimestamp":"Mon Aug 23 16:48:40 IST 2010",
         "low":-66.04000424423522,
         "marketCap":-9334516.42324327,
         "name":"WSO2 Company",
         "open":-64.61950137150009,
         "peRatio":-19.78600441437058,
         "percentageChange":5.411779328273005,
         "prevClose":71.2075112994578,
         "symbol":"WSO2",
         "volume":16842
      }
   }
}

For more information about using JSON with the ESB, see JSON Support. For an example of how to convert JSON to XML using using JavaScript instead of XSLT, see Sample 441: Converting JSON to XML Using JavaScript.