Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Date Time Format Conversion with XSLT Mediator in WSO2 ESB

DZone's Guide to

Date Time Format Conversion with XSLT Mediator in WSO2 ESB

Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

I recently came across this requirement where a xsd:datetime in the payload is needed to be converted to a different date time format as follows,

Original format : 2015-01-07T09:30:10+02:00
Required date: 2015/01/07 09:30:10

In WSO2 ESB, I found that this transformation can be achieved through a XSLT mediator, class mediator or a script mediator. In an overview, XSLT mediator uses a XSL stylesheet to format the xml payload passed to the mediator whereas in class mediator and script mediator we use java code and javascript code respectively to manipulate the message context. In this blog post I am going to present how this transformation can be achieved by means of the XSLT mediator.

XSL Stylesheet

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

<localEntry xmlns="http://ws.apache.org/ns/synapse" key="dateTime.xsl">
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
        <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
        <xsl:param name="date_time" />
        <xsl:template match="/">
            <dateTime>
                <required>
                    <xsl:value-of select="format-dateTime(xs:dateTime($date_time), '[Y0001]/[M01]/[D01] [H01]:[m01]:[s01] [z]')" />
                </required>
            </dateTime>
        </xsl:template>
    </xsl:stylesheet>
    <description />
</localEntry>


Proxy configuration

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="DateTimeTransformation" transports="https http" startOnLoad="true" trace="disable">
    <target>
        <inSequence>
            <property name="originalFormat" expression="$body/dateTime/original" />
            <xslt key="dateTime.xsl">
                <property name="date_time" expression="get-property('originalFormat')" />
            </xslt>
            <log level="full" />
        </inSequence>
    </target>
</proxy>

dateTime.xsl XLS style sheet is stored as an inline xml local entry in ESB.

In the proxy, the original date is passed as an parameter ("date_time") to the XLS style sheet. I have used format-dateTime function, a function of XSL 2.0, to do the transformation.

Sample request

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Header />
    <soap:Body>
        <dateTime>
            <original>2015-01-07T09:30:10+02:00</original>
        </dateTime>
    </soap:Body>
</soap:Envelope>

Console output

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
    <soap:Body>
        <dateTime xmlns="http://ws.apache.org/ns/synapse" xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <required>2015/01/07 09:30:10 GMT+2</required>
        </dateTime>
    </soap:Body>
</soap:Envelope>

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:

Published at DZone with permission of Kalpa Welivitigoda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}