2
votes

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

I used xml :

2015-01-06T17:51:01.67+05:30

My xslt file :

select="format-dateTime(TravellerRequest/RequestDate,'[M01]/[D01]/[Y0001]')"/>

in html page getting error :

Exception Details: System.Xml.Xsl.XsltException: 'format-dateTime()' is an unknown XSLT function.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I need output like this: 01/06/2015

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

plz guide me.

Thanks, Ram...

2
Which version of XSLT are you working with? - Eduardo Yáñez Parareda
Hi , i written xslt like this , please help me. My xslt file : <xsl:stylesheet xmlns:xsl="w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="html"/> <xsl:template match="TRSummary"> <xsl:value-of select="format-dateTime(TravellerRequest/RequestDate,'[M01]/[D01]/[Y0001]')"/> </xsl:template> </xsl:stylesheet> - lakshman kumar
I used xml : <TRSummary> <TravellerRequest> <RequestDate>2015-01-06T17:51:01.67+05:30</RequestDate> </TravellerRequest> </TRSummary> - lakshman kumar
in html page getting error : Exception Details: System.Xml.Xsl.XsltException: 'format-dateTime()' is an unknown XSLT function. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. I need output like this: 01/06/2015 How to convert the above datetime format into mm/dd/yyyy. - lakshman kumar
You should edit your question with this information, rather than put it in comments. To put code in the question, just copy it in, highlight it, and click the {} button to format it as code to make it readable. Alternatively, just put 4 spaces before each line of code (as that is what the {} button does). Thank you. - Tim C

2 Answers

1
votes

format-dateTime() is an XSLT 2.0 function. Your error message suggests that you are using an XSLT 1.0 processor. XSLT 1.0 does not recognize dates as such - but you can use string functions to rearrange the date to the required format:

<xsl:value-of select="substring(TravellerRequest/RequestDate, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(TravellerRequest/RequestDate, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(TravellerRequest/RequestDate, 1, 4)"/>

Note: If you're using a Microsoft processor (as suggested by Martin Honnen), see: https://msdn.microsoft.com/en-us/library/ms256099%28v=vs.110%29.aspx

0
votes

The function requires an XSLT 2.0 processor like Saxon 9 or XmlPrime. Based on the message System.Xml.Xsl.XsltException, you seem to use a Microsoft XSLT processor, Microsoft only supports XSLT 1.0. So you will need to change your application to use a third party XSLT 2.0 processor or you need to look into using a .NET extension object or function with Microsoft's XSLT processor, to delegate the formatting to .NET code, not to XSLT code.