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: 53

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

Thu Jun 26, 2014 9:34 pm

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: 2443

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

Thu Jun 26, 2014 9:50 pm

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: 53

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

Thu Jun 26, 2014 11:51 pm

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: 2443

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

Fri Jun 27, 2014 11:54 am

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

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 1 guest