lakshman kumar
lakshman kumar

Reputation: 29

How to Convert date format in xslt

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...

Upvotes: 2

Views: 5033

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

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

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167401

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.

Upvotes: 0

Related Questions