【问题标题】:How to convert xs:dateTime to xs:string in xslt如何在 xslt 中将 xs:dateTime 转换为 xs:string
【发布时间】:2020-10-22 18:27:16
【问题描述】:

我们正在尝试将日期时间从 GMT 转换为 EST,以便我们遵循这种方法。

  1. 我们在字符串中获取没有时区的日期时间,然后将时区附加到它。
  2. 附加时区后,将 DateTime 从 GMT 转换为 EST。
  3. adjust-dateTime-to-timezone 将返回 EST 中的日期时间 时区。
  4. 现在,我们希望从该模板中以字符串形式返回不带时区的 EST 日期时间。

这是我的 xsl -

<xsl:template name="convertGMTToEST">
          <xsl:param name="gmtDateTime" />
            <xsl:variable name="gmtDateTimeWithTimeZone" select="concat($gmtDateTime,'+00:00')"/>
            <xsl:variable name="estDateTime" select="adjust-dateTime-to- 
           timezone(xs:dateTime($gmtDateTimeWithTimeZone),xs:dayTimeDuration('-PT5H'))"/>
            
            <xsl:value-of select="substring-before($estDateTime, '-')"/>
    </xsl:template>

预期输出- 我们想在 xs:String 中返回没有时区的 EST dateTime。
我们如何在执行 substring-before 之前将 xs:dateTime 转换为 xs:string 作为 fn:substring-before() 的第一个参数是 xs: string;
注意- 我们使用的是 xslt 2.0 处理器.

【问题讨论】:

标签: xml datetime xslt xpath saxon


【解决方案1】:

“我们如何将 xs:dateTime 转换为 xs:string”这个问题的直接答案是:使用 string() 函数。但正如@michael.hor257k 指出的那样,我认为你让事情变得不必要地复杂。

【讨论】:

    【解决方案2】:

    我认为你过于复杂了。 GMT 和 EST 之间的差异是恒定的 5 小时。为什么不简单地从给定的 dateTime 中减去 5 小时并完成?例如:

    XML

    <input>
        <string>2020-01-01T20:45:15</string>
        <string>2020-01-01T04:15:30</string>
    </input>
    

    XSLT 2.0

    <xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/input">
       <output>
            <xsl:for-each select="string">
                <string>
                    <xsl:value-of select="xs:dateTime(.) - xs:dayTimeDuration('PT5H')"/>
                </string>
            </xsl:for-each>
        </output>    
    </xsl:template>
    
    </xsl:stylesheet>
    

    结果

    <?xml version="1.0" encoding="UTF-8"?>
    <output>
       <string>2020-01-01T15:45:15</string>
       <string>2019-12-31T23:15:30</string>
    </output>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多