在XSLT中以YYYY-MM-DD格式格式化日期

时间:2018-08-02 19:52:38

标签: xml xslt

我想将日期的XML数据转换为YYYY-MM-DD格式。这是我写的代码

<xsl:variable name="Formatted_Date" select="format-date(wd:continuous_service_date,'[Y0001]-[M01]-[D01]')"/>
<xsl:value-of select="substring($Formatted_Date,1,10)"/>`

但是它的输出仍然是MM/DD/YYYY
如何在XSLT中做到这一点?

1 个答案:

答案 0 :(得分:0)

不幸的是,您不能为此使用format-date(...)函数,因为它是XSLT-2.0函数。 XSLT-1.0中有一些处理此问题的方法,但是每种方法都有其特定之处。

例如,有

和其他一些。


对于您的特定情况,以下XSLT代码段可以满足您的需求:

<xsl:variable name="oldDateFormat" select="wd:continuous_service_date" />       
<xsl:variable name="year"  select="substring-after(substring-after($oldDateFormat,'/'),'/')" />
<xsl:variable name="month" select="substring-before($oldDateFormat,'/')" />
<xsl:variable name="day"   select="substring-before(substring-after($oldDateFormat,'/'),'/')" />
<xsl:value-of select="concat($year,'-',$month,'-',$day)"/>

其输出为

2005-10-14
相关问题