Getting compile error while using format-date() method in XSLT

Jyothi picture Jyothi · Apr 4, 2013 · Viewed 8.5k times · Source

I am trying to convert a XML to CSV using XSLT with version 2.0. But I get a compile error while using format-date method in XSLT. Following is the error:

Error checking type of the expression 'funcall(format-date, [variable-ref(dt/string), literal-expr([D01]/[M01]/[Y0001])])'.
FATAL ERROR: 'Could not compile stylesheet'.

XSLT code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"       xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates-and-times"
            extension-element-prefixes="date" date:doc="http://www.exslt.org/date">

<xsl:output method="text"/>

<xsl:template match="/">    
<xsl:variable name="dt" select="'2013-04-04'"/>
<xsl:value-of select="format-date($dt, '[D01]/[M01]/[Y0001]')" />  
<xsl:value-of select="format-date(current-date(), '[D01]/[M01]/[Y0001]')" />               

</xsl:template>
</xsl:stylesheet>

Answer

Michael Kay picture Michael Kay · Apr 4, 2013

Your variable $dt is a string, not a date. You need to convert it to a date using xs:date('2013-04-04') before you can format it.