Monday 23 November 2015

String To Time Stamp Conversion using XSLT


            This post shows about String To Time Stamp Conversion using XSLT /Transformation in BPEL process which i have done in my real time experience.
     
            The requirement looks like we are getting time as a string from source and we need to convert to time stamp (hh:mm:ss). for example, from source the value will be "102530" and the response should be "10:25:30". This can be done by using substring() function. But after 12:00 AM, the request value will be like "30" or "525" and response should be like "00:00:30" or "00:05:25".

            In order to get this type of response i wrote a logic in XSLT as below.

XSLT code :

<xsl:variable name="StringToTime">
      <xsl:choose>
        <xsl:when test="/ns0:process/ns0:input">
          <xsl:variable name="Temp">
            <xsl:value-of select="substring(concat('000000', /ns0:process/ns0:input), string-length(/ns0:process/ns0:input)+1)"/>
          </xsl:variable>
          <xsl:value-of select="concat(substring($Temp,1,2),':',substring($Temp,3,2),':',substring($Temp,5,2))"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="/ns0:process/ns0:input"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>


Request :

<?xml version="1.0" encoding="UTF-8" ?>
<process xmlns="http://xmlns.oracle.com/DemoApplication/StringtoTimeConversion/BPELProcess">
   <input>525</input>
</process>

Response :

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:processResponse xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:ns0="http://xmlns.oracle.com/DemoApplication/StringtoTimeConversion/BPELProcess">
   <ns0:result>00:5:25</ns0:result>
</ns0:processResponse>


Thanks!!