通过 XSLT 在 XML 中格式化日期 [英] Format a date in XML via XSLT

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

问题描述

当我使用 XML 序列化器序列化一个 DateTime 时,它的编写格式如下:

When I use the XML serializer to serialize a DateTime, it is written in the following format:

<Date>2007-11-14T12:01:00</Date>

当通过 XSLT 样式表传递它以输出 HTML 时,我该如何格式化它?在大多数情况下,我只需要日期,当我需要时间时,我当然不希望出现有趣的 T".

When passing this through an XSLT stylesheet to output HTML, how can I format this? In most cases I just need the date, and when I need the time I of course don't want the "funny T" in there.

推荐答案

这里有几个 1.0 模板可供您使用:-

Here are a couple of 1.0 templates that you can use:-

<xsl:template name="formatDate">
    <xsl:param name="dateTime" />
    <xsl:variable name="date" select="substring-before($dateTime, 'T')" />
    <xsl:variable name="year" select="substring-before($date, '-')" />
    <xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
    <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
    <xsl:value-of select="concat($day, ' ', $month, ' ', $year)" />
</xsl:template>

<xsl:template name="formatTime">
    <xsl:param name="dateTime" />
    <xsl:value-of select="substring-after($dateTime, 'T')" />
</xsl:template>

打电话给他们:-

    <xsl:call-template name="formatDate">
        <xsl:with-param name="dateTime" select="xpath" />
    </xsl:call-template>

    <xsl:call-template name="formatTime">
        <xsl:with-param name="dateTime" select="xpath" />
    </xsl:call-template>

其中 xpath 是具有标准日期时间格式的元素或属性的路径.

where xpath is the path to an element or attribute that has the standard date time format.

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

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