使用xslt 2.0将日期从DD-MMM-YYYY转换为YYYY-MMM-DD

时间:2019-08-07 11:20:26

标签: xslt-2.0

我有一个XML,其日期格式为(2019-Aug-19)(YYYY-MMM-DD)。

我希望将其转换为(2019-08-19)(YYYY-MM-DD)

听力是我所做的一个例子。但是没有运气

<xsl:value-of select="format-dateTime(xs:dateTime(concat(date, T00:00:00Z')), '[D01]-[M01]-[Y0001]')"/>

1 个答案:

答案 0 :(得分:1)

这是另一种简单的方法:

XML:

<time>2019-Aug-19T00:00:00Z</time>

XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">

    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:variable name="dt" select="time"/>
    <xsl:template match="/">
      <xsl:value-of select="format-dateTime(xs:dateTime(replace($dt, substring-before(substring-after($dt,'-'),'-') ,format-number(index-of(('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov', 'dec'),lower-case(substring-before(substring-after(lower-case($dt),'-'), '-'))),'00'))),'[D01]-[M01]-[Y0001]')"/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

结果:

19-08-2019

对于YYYY-MM-DD,您可以执行以下操作:

<xsl:value-of select="format-dateTime(xs:dateTime(replace($dt, substring-before(substring-after($dt,'-'),'-') ,format-number(index-of(('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov', 'dec'),lower-case(substring-before(substring-after(lower-case($dt),'-'), '-'))),'00'))),'[Y0001]-[M01]-[D01]')"/>

结果:

2019-08-19

请参阅链接:http://xsltransform.net/nbiCsZm