【问题标题】:Format Date and Time in XSLT在 XSLT 中格式化日期和时间
【发布时间】:2014-06-06 19:07:22
【问题描述】:

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

<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)"/>

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

有什么建议吗?

谢谢。

【问题讨论】:

    标签: xml datetime xslt format


    【解决方案1】:

    你需要更加努力:

    <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 来解决此问题。

    这是一个例子。我将replace() 放在xsl:variable 中以使其更易于阅读。不过这不是必需的。

    XML 输入

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

    输出

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

    您可以在此处找到有关图片字符串(format-dateTime() 的第二个参数)的更多信息:

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

    【讨论】:

      【解决方案3】:

      这些变量将为您提供日期(采用 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>
      

      然后您将获得所需格式的日期。要打印时间和日期之间的空格,您可以使用&lt;xsl:text&gt;

      <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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多