加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 在xslt中格式化日期

发布时间:2020-12-16 22:42:01 所属栏目:百科 来源:网络整理
导读:我正在尝试在xslt中格式化某个日期格式.这是格式: 2012-7-19 我想将它转换为xslt中的数值进行排序,以便上面的内容变为: 20120719 问题是单个月/天的前面没有0.所以我需要以某种方式在单个数字月/日前添加,但不要在有两位数的月/日前添加.有谁知道我怎么做
我正在尝试在xslt中格式化某个日期格式.这是格式:

2012-7-19

我想将它转换为xslt中的数值进行排序,以便上面的内容变为:

20120719

问题是单个月/天的前面没有0.所以我需要以某种方式在单个数字月/日前添加,但不要在有两位数的月/日前添加.有谁知道我怎么做到这一点?

到目前为止我有这个:

<xsl:value-of select="concat(
    substring(substring-after(substring-after(./AirDate,'/'),5),substring(substring-after(./AirDate,3),substring-before(./AirDate,'/'))"/>

但偶尔会出现一位数的天数,并且不会在单个数字的月份/天之前输入0

我没有能力在将数据源传递给xslt之前更改数据源,我必须使用xslt 1.0版.

解决方法

我认为format-number可以解决问题.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:variable name="date" select="'2007-3-19'" />
    <xsl:template name="format_date" >
        <xsl:param name ="date" />
        <xsl:variable name ="year" select="substring-before($date,'-')" />
        <xsl:variable name ="month_and_day" select="substring-after($date,'-')" />
        <xsl:variable name ="day" select="substring-after($month_and_day,'-')" />
        <xsl:variable name ="month" select="substring-before($month_and_day,'-')" />
        <xsl:value-of select="$year"/>
        <xsl:value-of select="format-number($month,'00')"/>
        <xsl:value-of select="format-number($day,'00')"/>
    </xsl:template>

    <xsl:template match="/" >
        <xsl:call-template name="format_date" >
            <xsl:with-param name ="date" select="$date"/>
        </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读