【问题标题】:XSLT convert dateTime with default timezoneXSLT 将 dateTime 转换为默认时区
【发布时间】:2013-10-17 12:11:48
【问题描述】:

我的输入 XML 01.08.2013 11:22 中有一个日期,需要转换成这种格式 2013-08-01T11:22:00+02:00

我的输入 XML:

<?xml version="1.0"?>
<input>01.08.2013 11:22</input>

期望的输出

<?xml version="1.0"?>
<output>2013-08-01T11:22:00+02:00</output>

到目前为止我做了什么:

  1. 01.08.2013 11:22 转换为 xs:dateTime OK(使用我自己的函数)
  2. 将标准 xs:dateTime 转换为 2013-08-01T11:22:00+02:00 在这里我失败了,因为我没有定义时区。我需要使用服务器默认时区(夏季夏令时可能会更改)

首先我搜索了一个标准操作,但没有找到任何东西。所以我创建了自己的基于字符串的函数,将输入转换为标准日期,这是有效的。

但是现在我的输出 2) 不起作用,因为我没有时区,所以我的输出总是这个 2013-8-1T11:22:00 没有时区....

如何将时区添加到日期,以便我的输出正确?

或者一般来说,如何将01.08.2013 11:22 转换为2013-08-01T11:22:00+02:00

<xsl:template name="convertDate">
    <xsl:param name="givenDate"/>
    <!-- a) Convert `01.08.2013 11:22` to a xs:dateTime  -->
    <xsl:variable name="dd" select="substring-before($givenDate, '.')"/>
    <xsl:variable name="mmRest" select="substring-after($givenDate, '.')"/>
    <xsl:variable name="mm" select="substring-before($mmRest, '.')"/>
    <xsl:variable name="yyyyRest" select="substring-after($mmRest, '.')"/>
    <xsl:variable name="yyyy" select="substring-before($yyyyRest, ' ')"/>
    <xsl:variable name="hhRest" select="substring-after($yyyyRest, ' ')"/>
    <xsl:variable name="hh" select="substring-before($hhRest, ':')"/>
    <xsl:variable name="min" select="substring-after($hhRest, ':')"/>
    <xsl:variable name="correctDate" select="xs:dateTime(concat($yyyy,'-',$mm,'-',$dd,'T',$hh,':',$min,':','00'))"/>        

    <!-- b) Convert the standard xs:dateTime to `2013-08-01T11:22:00+02:00`  -->
    <xsl:value-of select="format-dateTime($correctDate,'[Y]-[M]-[D]T[H]:[m]:[s][z]')"/>
</xsl:template>

【问题讨论】:

  • 由于日期时间/时区很难(确实如此),我强烈建议不要使用字符串函数来处理它们。如果您说您使用的是什么 XSLT 处理器,那么有人可以推荐一个日期/时间功能(扩展)功能来做正确的事情。

标签: datetime xslt


【解决方案1】:

&lt;xsl:value-of select="current-dateTime()"/&gt; 是一个XPATH 2.0 Function 并返回时区呢?

【讨论】:

  • 但是我不需要当前的dateTime,dateTime在输入xml中给出。是否可以使用 current-dateTime() 进行计算以获得我想要的输出? givenDateTime+current-dateTime()-current-DateTime() 将再次给出 givenDateTime - 但可能与时区有关?
  • 只需使用输出中的子字符串:2006-04-10T13:40:23.83-05:00 最后六个字符是运行 xslt/xquery 的服务器的时区。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 2011-03-28
  • 1970-01-01
  • 2011-02-02
  • 2017-11-23
  • 1970-01-01
相关资源
最近更新 更多