How do I convert a string variable date into a date?

Questions about XML that are not covered by the other forums should go here.
winkimjr2
Posts: 62
Joined: Thu Jun 26, 2014 9:00 pm

How do I convert a string variable date into a date?

Post by winkimjr2 »

I have a xslt which is using a xml document. The xml document has a variable dateServed="06/20/2014". When I run the xslt, I get error FORG0001: Cannot conver xs:string with value '06/20/2014' to xs:date.

What do I need to do to convert this dateServed="06/20/2014" to a date?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: How do I convert a string variable date into a date?

Post by adrian »

Hi,

xs:date expects the date to be in format yyyy-mm-dd, so you just have to adjust it to that format with the help of string functions.
There is an example showing this on stack overflow:
http://stackoverflow.com/questions/1685 ... n-xslt-2-0

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
winkimjr2
Posts: 62
Joined: Thu Jun 26, 2014 9:00 pm

Re: How do I convert a string variable date into a date?

Post by winkimjr2 »

I am still confused as to how to do this.
I have the following from xsl

Code: Select all

<NotificationEvent dateServed="06/20/2014"</NotificationEvent>
So how do I convert this date string to date?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: How do I convert a string variable date into a date?

Post by adrian »

For creating an xs:date (yyyy-mm-dd) from dateServed='mm/dd/yyyy':

Code: Select all

<xsl:variable name="date" select="xs:date(concat(
substring(@dateServed,7,4),'-',
substring(@dateServed,1,2),'-',
substring(@dateServed,4,2)))"/>
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply