Friday, 5 February 2016

XSLT - Transform Date format (YYYYMMDD to DD-MMM-YYYY)

Date Format Transformation Using XSLT



Sometimes in we may require to Transform date from one format to another format. Because, Source is sending date in a format which target does not accepts.

So here is an simple XSLT code which transforms Date from YYYYMMDD to DD-MMM-YYYY.
Actually this can be modified to suit your needs of any other format as well.

This XSLT can be used in SOA 11g, OSB 11g, SOA 12c, OSB 12c and could be used in any other application which supports XSL transform.

Code Snippet :


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0">
  <xsl:template match="/">
  <Root>
    <Values>            
            
            <Value AttributeID="Description">              
                <xsl:value-of select="/Header/Description"/>              
            </Value>
           
           <xsl:choose>
            <xsl:when test="string-length(/Header/StartDateActive) = 8" >
                 <Value AttributeID="Introduction Date">
                      <xsl:call-template name="date">
                           <xsl:with-param name="yyyymmdd" select="/Header/StartDateActive"/>
                      </xsl:call-template>
                  </Value>
              </xsl:when>
          <xsl:otherwise>
                  <Value AttributeID="Introduction Date"></Value>
              </xsl:otherwise>
          </xsl:choose>    
            

            
     </Values>
    </Root>
  </xsl:template>
  
  <xsl:template name="date">
    <xsl:param name="yyyymmdd"/>
    <xsl:variable name="yyyy" select="substring($yyyymmdd, 1, 4)"/>       
    <xsl:variable name="mm" select="substring($yyyymmdd, 5, 2)"/>
    <xsl:variable name="dd" select="substring($yyyymmdd, 7, 2)"/>
   

  <xsl:value-of select=" concat($dd,'-') "/>
    <xsl:choose>
      <xsl:when test="$mm = '01'">Jan</xsl:when>
      <xsl:when test="$mm = '02'">Feb</xsl:when>
      <xsl:when test="$mm = '03'">Mar</xsl:when>
      <xsl:when test="$mm = '04'">Apr</xsl:when>
      <xsl:when test="$mm = '05'">May</xsl:when>
      <xsl:when test="$mm = '06'">Jun</xsl:when>
      <xsl:when test="$mm = '07'">Jul</xsl:when>
      <xsl:when test="$mm = '08'">Aug</xsl:when>
      <xsl:when test="$mm = '09'">Sep</xsl:when>
      <xsl:when test="$mm = '10'">Oct</xsl:when>
      <xsl:when test="$mm = '11'">Nov</xsl:when>
      <xsl:when test="$mm = '12'">Dec</xsl:when>
    </xsl:choose>
    <xsl:value-of select=" concat ('-', $yyyy) "/>
</xsl:template>

</xsl:stylesheet>



Testing the XSLT :

Input :

<Header>   <Description>My Description</Description>   <StartDateActive>20160206</StartDateActive></Header>

Output :


<Root>

  <Values>
<Value AttributeID="Description">My Description</Value>
<Value AttributeID="Introduction Date">06-FEB-2016</Value>  
  </Values>
</Root>


No comments:

Post a Comment