Srikrishna Sharma
Srikrishna Sharma

Reputation: 171

Converting Date Time in XSLT

I am trying to convert a date in yyyy/mm/dd format using xslt.


"dateOfBirth": "</xsl:text><xsl:value-of select="format-date(dateOfBirth, '[Y0001]/[M01]/[D01]')" /><xsl:text>",

but I am getting this error

Unable to generate the XML document using the provided XML/XSL input. Invalid date "01/21/1950" (When year exceeds 4 digits, leading zeroes are not allowed)

my dateOfBirth value is always in mm-dd-yyyy format.

Can anyone tell me what i am doing wrong? I have searched various answers on stackoverflow which are mainly of string replacment. Since I have around 10 dates in my xml Is there any one linear approach to do so or any inbuilt function for doing the same. Any help is appreciated.

Thanks!

Upvotes: 0

Views: 326

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167781

If you use XSLT 2 or 3 with Saxon 9 you can always write your own XSLT function with xsl:function that does e.g.

$input => replace('([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$1-$2') => xs:date() => format-date('[Y0001]/[M01]/[D01]')

(XSLT 3) or

format-date(xs:date(replace($input, '([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$1-$2')), '[Y0001]/[M01]/[D01]')

(XSLT 2 and 3)

Upvotes: 2

Related Questions