Home > other >  How to convert dateTimes in XSLT?
How to convert dateTimes in XSLT?

Time:11-03

I am using XSLT to parse Wordpress feed. But have a problem with some date format convert. I need to pars <pubDate>Mon, 11 Oct 2021 13:23:10 0000</pubDate> to 2021-10-11T13:23:10z . I have used

                <xsl:element name="field">
                    <xsl:attribute name="name">ds_date_created</xsl:attribute>
                    <xsl:value-of select="format-date(pubDate,'[Y0001]-[M01]-[D01]')"/>
                </xsl:element>

xslt format-date function, but getting this message:

    Validation error at xsl:value-of on line 61 
      FORG0001: Invalid date "Mon, 01 Nov 2021 13:45:01  000..." (Non-numeric year component)
     at xsl:apply-templates (#23)
     processing /rss/channel[1]/item[1]

Are this date can be converted by xslt? Is there any way?

CodePudding user response:

In XSLT 3 (e.g. Saxon 9.8 or later, Saxon-JS 2, Altova XML 2017 R3 and later) you can use parse-ietf-date to parse that format, I think: parse-ietf-date('Mon, 11 Oct 2021 13:23:10 0000'). That should give you an xs:dateTime, so for formatting it use format-dateTime.

CodePudding user response:

You could do it "manually", e.g. like this: https://gist.github.com/aendrew/eaa220e619008cbb628c

If you copy-paste the linked template definition, you can call it with:

<xsl:call-template name="format-from-rfc-to-iso">
    <xsl:with-param name="rfc-date" select="pubDate" />
</xsl:call-template>

instead of your <xsl:value-of

  • Related