How to parse string to date in xslt 2.0

Like Tomalak said, you can use substring() and concat() to build a string you can cast as an xs:date() (It doesn’t sound like you want a dateTime.)

Example:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="in" select="'30042013'"/>

    <xsl:template match="https://stackoverflow.com/">
        <xsl:variable name="date" select="xs:date(concat(
            substring($in,5,4),'-',
            substring($in,3,2),'-',
            substring($in,1,2)))"/>
        <xsl:value-of select="format-date($date,'[MNn] [D], [Y]')"/>
    </xsl:template>

</xsl:stylesheet>

produces (with any XML input)

April 30, 2013

Leave a Comment