首页 文章

在XSLT中格式化日期和时间

提问于
浏览
1

我有一个列以默认格式返回日期和时间 .

<xsl:value-of select="/CAudioFile/CRI/LocalStartTime"/>

哪个回报

2014-05-08T08:01:26.4000000-0700

我想把这个时间用这种格式:

05/08/2014 08:01:26

我试图使用不同的子串组合来获取格式:

<xsl:value-of select="substring(/CAudioFile/CRI/LocalStartTime,1,10)"/>

但它要么只返回日期,要么只返回时间 .

有什么建议?

谢谢 .

3 回答

  • 3

    你需要更加努力地工作:

    <xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 6, 2)"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 9, 2)"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 1, 4)"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="substring(CAudioFile/CRI/LocalStartTime, 12, 8)"/>
    
  • 2

    您没有指定XSLT的版本 . 如果可以使用2.0,则可以使用 format-dateTime() .

    在你的情况下,由于时区,它可以作为 xs:dateTime 施放 . 时区需要2位数; 07:00 而不是 0700 . 您可以使用 replace() 并将其转换为 xs:dateTime 来解决此问题 .

    这里's an example. I'已将 replace() 放在 xsl:variable 中,以便于阅读 . 但这不是必需的 .

    XML Input

    <test>2014-05-08T08:01:26.4000000-0700</test>
    

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="/*">
            <xsl:variable name="dt" select="xs:dateTime(replace(normalize-space(.),'([+-]\d{2})(\d{2})$','$1:$2'))" as="xs:dateTime"/>
            <xsl:copy>
                <xsl:value-of select="format-dateTime($dt,'[M00]/[D00]/[Y] [H00]:[m00]:[s00]')"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Output

    <test>05/08/2014 08:01:26</test>
    

    你可以在这里找到关于图片字符串的更多信息(第二个arg of format-dateTime() ):

    http://www.w3.org/TR/xslt20/#date-picture-string

  • 0

    这些变量将为您提供日期(采用ISO 8601格式)和使用XPath 1.0表达式的时间:

    <xsl:variable name="date">
        <xsl:value-of select="substring-before(.,'T')"/>
    </xsl:variable>
    <xsl:variable name="time">
        <xsl:value-of select="substring-before(substring-after(.,'T'),'.')"/>
    </xsl:variable>
    

    要将日期转换为mm / dd / yyyy格式,可以将此模板添加到样式表中:

    <xsl:template name="iso-to-mdy">
        <xsl:param name="iso-date"/>
        <xsl:variable name="year" select="substring($iso-date,1,4)"/>    
        <xsl:variable name="month" select="substring($iso-date,6,2)"/>
        <xsl:variable name="day" select="substring($iso-date,9,2)"/>
        <xsl:value-of select="$month"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="$day"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="$year"/>
    </xsl:template>
    

    而是像这样声明你的 date 变量,将ISO 8601日期作为参数传递:

    <xsl:variable name="date">
        <xsl:call-template name="iso-to-mdy">
            <xsl:with-param name="iso-date" select="substring-before(.,'T')"/>
        </xsl:call-template>
    </xsl:variable>
    

    然后,您将获得所需格式的日期 . 要在时间和日期之间打印空格,您可以使用 <xsl:text>

    <date>
        <xsl:value-of select="$date"/>
        <xsl:text> </xsl:text>
        <xsl:value-of select="$time"/>
    </date>
    

    这将打印

    <date>05/08/2014 08:01:26</date>
    

    XSLT Fiddle

相关问题