首页 > 解决方案 > 使用 xslt 2.0 将日期从 DD-MMM-YYYY 转换为 YYYY-MMM-DD

问题描述

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

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

听到是我所做的一个样本。但没有运气

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

标签: xslt-2.0

解决方案


在这里你可能是另一种简单的方法:

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


推荐阅读