Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


How to convert date format in xslt. I have created application as shown below

I used xml :
--------------
XML
<TRSummary>
  <TravellerRequest>
    <TravelRequestNumber>338</TravelRequestNumber>
    <OriginatorName>SureshVL RonVL</OriginatorName>
        <RequestDate>2015-01-06T17:51:01.67+05:30</RequestDate>
    <CostCenter>800</CostCenter>
    <ProjectManagerName>Prasannavl Athinavl Athinavl</ProjectManagerName>
    <JobNumber>1234</JobNumber>
    <ElementNumber>1234</ElementNumber>
    <Remarks>remarks</Remarks>
    <ExpectedTravelCost>1200</ExpectedTravelCost>
    <PMC>Industry international</PMC>
  </TravellerRequest>
</TRSummary>



My xslt file :
--------------

XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy� -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
     <xsl:for-each select="TRSummary">
      <html>
        <body>
          <table>
            <tr>
              <td>
            <xsl:value-of select="TravellerRequest/RequestDate"/>
              </td>
            </tr>
          </table>
        </body>
      </html>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>


in html page getting out put like this :
---------------------------------------

Request Date: 2015-01-07T15:19:23.52+05:30

How to convert the above datetime format into mm/dd/yyyy.

i need output like this: 01/07/2015.

plz guide me.



Thanks,
Ram...
Posted
Updated 27-May-20 12:20pm
v2
Comments
Zoltán Zörgő 11-Jan-15 6:52am    
Any progress? If you have found my extensive answer of use, please accept it formally.

Here you have more possibilities depending on XSLT version: http://blog.fpmurphy.com/2008/05/xslt-datetime-formatting.html[^]

Supposing following structure of the XML:
XML
<TRSummary>
  <TravellerRequest>
    <TravelRequestNumber>338</TravelRequestNumber>
    <OriginatorName>SureshVL RonVL</OriginatorName>
        <RequestDate>2015-01-06T17:51:01.67+05:30</RequestDate>
    <CostCenter>800</CostCenter>
    <ProjectManagerName>Prasannavl Athinavl Athinavl</ProjectManagerName>
    <JobNumber>1234</JobNumber>
    <ElementNumber>1234</ElementNumber>
    <Remarks>remarks</Remarks>
    <ExpectedTravelCost>1200</ExpectedTravelCost>
    <PMC>Industry international</PMC>
  </TravellerRequest>
  <TravellerRequest>
    <TravelRequestNumber>338</TravelRequestNumber>
    <OriginatorName>SureshVL RonVL</OriginatorName>
        <RequestDate>2015-01-06T17:51:01.67+05:30</RequestDate>
    <CostCenter>800</CostCenter>
    <ProjectManagerName>Prasannavl Athinavl Athinavl</ProjectManagerName>
    <JobNumber>1234</JobNumber>
    <ElementNumber>1234</ElementNumber>
    <Remarks>remarks</Remarks>
    <ExpectedTravelCost>1200</ExpectedTravelCost>
    <PMC>Industry international</PMC>
  </TravellerRequest>
</TRSummary>

This is one option:
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <html>
       <body>
          <table border="1">
            <xsl:apply-templates select="TRSummary/TravellerRequest"/>
          </table>
       </body>
    </html>
  </xsl:template>

  <xsl:template match="RequestDate">
    <td>
      <xsl:value-of select="format-dateTime(.,'[M01]/[D01]/[Y0001]')" />
    </td>
  </xsl:template>

  <xsl:template match="TravellerRequest">
     <tr>
       <xsl:apply-templates select="RequestDate"/>
     </tr>
   </xsl:template>

</xsl:stylesheet>

[Update]
This is also working, but not so neat:
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
      <html>
        <body>
          <table>
            <xsl:for-each select="TRSummary/TravellerRequest">
             <tr>
              <td>
                <xsl:value-of select="format-dateTime(RequestDate,'[M01]/[D01]/[Y0001]')" />
               </td>
             </tr>
            </xsl:for-each>
          </table>
        </body>
      </html>
  </xsl:template>
</xsl:stylesheet>


[Update 2]
This works with XSLT 1.0 too:
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template name="formatdate">
     <xsl:param name="DateTimeStr" />

     <xsl:variable name="datestr">
         <xsl:value-of select="substring-before($DateTimeStr,'T')" />
     </xsl:variable>

     <xsl:variable name="mm">
         <xsl:value-of select="substring($datestr,6,2)" />
     </xsl:variable>

     <xsl:variable name="dd">
        <xsl:value-of select="substring($datestr,9,2)" />
     </xsl:variable>

     <xsl:variable name="yyyy">
        <xsl:value-of select="substring($datestr,1,4)" />
     </xsl:variable>

     <xsl:value-of select="concat($mm,'/', $dd, '/', $yyyy)" />
  </xsl:template>

  <xsl:template match="/">
      <html>
        <body>
          <table border="1">
            <xsl:for-each select="TRSummary/TravellerRequest">
             <tr>
              <td>
                <xsl:call-template name="formatdate">
                    <xsl:with-param name="DateTimeStr" select="RequestDate"/>
                </xsl:call-template>
               </td>
             </tr>
            </xsl:for-each>
          </table>
        </body>
      </html>
  </xsl:template>
</xsl:stylesheet>
 
Share this answer
 
v4
Comments
vulisiramu 8-Jan-15 8:29am    
I am not able to find the solution, plz could you help me.
Zoltán Zörgő 8-Jan-15 8:34am    
Have you read the article on the link?
vulisiramu 8-Jan-15 8:59am    
am not able to access link , because blocked this url in my office.
Zoltán Zörgő 8-Jan-15 10:02am    
See update...
Zoltán Zörgő 9-Jan-15 2:49am    
See also update for a solution more like your approach - but please note the reordering of the elements in the xslt! your foreach would result in many html tags and body.. which is invalid.
 
Share this answer
 
Comments
vulisiramu 8-Jan-15 8:03am    
I am not able to find the solution, plz could you help me.
vulisiramu 8-Jan-15 23:58pm    
Hi praveen, Plz provide answer to above question?
Praveen Kumar Upadhyay 9-Jan-15 1:15am    
format-date() is not working for you?
Zoltán Zörgő 9-Jan-15 2:39am    
And what about the solution I had given? It is tested ad working... please note that your approach won't be valid if more than one requests are in the xml. See update for a solution more like your approach - but please note the reordering of the elements in the xslt!
Praveen Kumar Upadhyay 9-Jan-15 7:30am    
Okay. Then OP is free to accept your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900