Search code examples
xmlxsltdate-format

How to format a date using XSLT?


Trying to convert date to specific format but getting error:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.openapplications.org/oagis/9"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:variable name="origDate" select="row[@num='6']/cell[@num='2']"/>
<PromisedDeliveryDateTime><xsl:value-of select="format-date(xs:date($origDate), '[Y] [M] [D]')" /></PromisedDeliveryDateTime>

But I get the error:

Failed to transform XQMessage part, index=0 (Invalid date "30.11.2011"
(Non-numeric component)


Solution

  • The xs:date() constructor function expects a string in ISO date format: 2011-11-30. To manipulate dates in other formats you first need to convert them to ISO format, which you can do for example as

    string-join(reverse(tokenize($in, '.')), '-')