【问题标题】:format and display datetime in xslt在 xslt 中格式化和显示日期时间
【发布时间】:2011-10-18 10:12:20
【问题描述】:

我有一个包含日期时间数据的 XML:

<A>
    <StartDate>2011-11-01T00:00:00</StartDate>
    <EndDate>2011-11-30T00:00:00</EndDate>
    <IsRecurring>false</IsRecurring>
</A>

我只需要在 xslt 中输入以下格式的日期:

01/11/2011 - 30/11/2011

当我这样做时:

<xsl:value-of select="A/StartDate/"> - <xsl:value-of select="A/EndDate/">

我明白了:

2011-11-01T00:00:00 - 2011-11-30T00:00:00

如何正确显示?

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    查看 XPath 字符串函数:substringsubstring-before

    参考:http://www.w3.org/TR/xpath/#section-String-Functions

    <xsl:variable name="dt" select="'2011-11-01T12:13:59'"/>
    
            <xsl:value-of select="concat(
                          substring($dt, 9, 2),
                          '/',
                          substring($dt, 6, 2),
                          '/',
                          substring($dt, 1, 4)
                          )"/>
    

    【讨论】:

      【解决方案2】:

      编辑:如果您仍然必须使用 XSLT 1.x,您可以查看 EXSLT's date:format-date user function。在这种情况下,模式是dd/MM/yyyy

      如果您使用的是 XSLT 2.0,使用format-dateTime 函数会更方便。

      这是您在 XSLT 2.0 中的示例:

      <xsl:value-of select="format-dateTime(A/StartDate, '[D01]/[M01]/[Y0001]')" /> 
      - <xsl:value-of select="format-dateTime(A/EndDate, '[D01]/[M01]/[Y0001]')" />
      

      【讨论】:

      • 我得到一个异常:“'format-dateTime()' 是一个未知的 XSLT 函数。”也许我不使用 XSLT2.0?我怎样才能检查它?我怎样才能改变它?
      • @Naor 你刚刚做了。只需使用 Kirill Polishchuk 回答即可。
      【解决方案3】:

      如果您使用的是 XSLT 2.0,除了 @Kirill Polishchuk 回答之外,您还可以简单地进行替换:

      substring(replace($input, "(\d{4})-(\d{2})-(\d{2})", "$3/$2/$1"), 0, 11)
      

      $input 是包含日期的节点的内容。

      【讨论】:

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