【问题标题】:How to convert String date time to DateTime in xslt如何在xslt中将字符串日期时间转换为日期时间
【发布时间】:2020-10-21 17:34:30
【问题描述】:

我们有 GMT 的日期时间,并希望将其转换为 EST。当我们尝试低于 xsl 时,我们得到一个错误。

FORG0001:无效的 dateTime 值“05/26/20 14:58”(非数字年份部分)

这是我的 xsl-

            <xsl:variable name="estDateTime">
                <xsl:call-template name="convertGMTToEST">
                <xsl:with-param name="gmtDateTime" select="'05/26/20 14:58'"/>
                </xsl:call-template>
            </xsl:variable>
            
    <xsl:template name="convertGMTToEST">
      <xsl:param name="gmtDateTime" />
        <xsl:value-of select="adjust-dateTime-to-timezone(xs:dateTime($gmtDateTime),xs:dayTimeDuration('-PT5H'))"/>
    </xsl:template>

预期输出- 我们希望将此日期时间转换为相应的 EST 日期时间。

注意- 我们使用的是 xslt 2.0 处理器。

【问题讨论】:

  • XPath/XSLT 2 及更高版本的xs:date 格式为例如2020-06-26xs:dateTime 格式,例如2020-06-26T14:58:00 所以你需要分解你得到的字符串输入以提取相关组件并重新排序/重新格式化它们以构造xs:dateTime 的正确输入格式。 substring 函数或 replace 函数或 tokenizeanalyze-string 调用所有帮助。

标签: xml xslt xpath saxon


【解决方案1】:

考虑使用saxon:parse-dateTime() 扩展函数:

https://www.saxonica.com/documentation/index.html#!functions/saxon/parse-dateTime

需要 Saxon-PE 或更高版本,Saxon 10 及更高版本。

【讨论】:

  • 我们使用的是撒克逊 9.9。使用 Saxon 9.9,我们有什么方法可以将 DateTime 从 GMT 转换为 EST 中的 DateTime?
  • 使用标准 XSLT 日期/时间库转换时区没有问题。它正在解析非标准的美国格式 m/d/y 日期表示,这就是问题所在。
【解决方案2】:

xs:dateTime(replace('05/26/20 14:58', '([0-9]{2})/([0-9]{2})/([0-9]{2}) ([0-9]{2}):([0-9]{2})', '20$3-$1-$2T$4:$5:00')) 可能适用于特定样本,但当然,如果输入格式有任何变化,时间部分有四位数年份或秒,您可能需要不止一个简单的替换调用。

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多