在 xslt 中格式化日期 [英] Formatting date in xslt

查看:52
本文介绍了在 xslt 中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 xslt 中格式化某个日期格式.这是格式:

I'm trying to format a certain date format in xslt. This is the format:

2012-7-19

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

I'd like to convert this to a numerical value in xslt for sorting, so that the above would become:

20120719

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

The problem is that single months/days don't have a 0 in front of them. So I need to somehow add that in front of single digit months/days, but not in front of months/days that have two digits. Does anyone know how I can do this?

到目前为止我有这个:

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

但偶尔会在个位数天数中输入 /,并且不会在个位数月数/天数前放置 0

but that occasionally throws in a / for single digit days, and doesn't put a 0 in front of single digit months/days

我无法在将数据源传递给 xslt 之前更改数据源,我必须使用 xslt 1.0 版.

I don't have the ability to change the data source before passing it to the xslt, and I have to use xslt version 1.0.

推荐答案

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

I think format-number will do the trick.

<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>

这篇关于在 xslt 中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆