如何在 xslt 中将 xs:dateTime 转换为 xs:string

How to convert xs:dateTime to xs:string in xslt

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

  1. 我们在字符串中获取没有时区的日期时间,然后将时区附加到它。
  2. 添加时区后,将 DateTime 从 GMT 转换为 EST。
  3. adjust-dateTime-to-timezone 将 return EST 中的日期时间与 时区。
  4. 现在,我们想要return EST date-time without timezone as String from this template。

这是我的 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>

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

我认为你把这个复杂化了。 GMT 和 EST 之间的差异是恒定的 5 小时。为什么不简单地从给定的日期时间中减去 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>

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