我有一个 XML 文档,其中包含标准 ISO 8601 格式的日期。像这样:
2011-11-29T04:15:22-08:00
我想使用 XSLT 将时间转换为 UTC 和以下形式的输出日期:
2011-11-29 12:15:22
如何实现?
提前致谢。
最佳答案
以下 XPath 2.0 表达式生成所需的字符串值:
translate(
string(
adjust-dateTime-to-timezone(
xs:dateTime('2011-11-29T04:15:22-08:00'),
xs:dayTimeDuration('PT0H')
)
),
'TZ',
' '
)
基于 XSLT 的验证:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:sequence select=
"translate(
string(
adjust-dateTime-to-timezone(
xs:dateTime('2011-11-29T04:15:22-08:00'),
xs:dayTimeDuration('PT0H')
)
),
'TZ',
' '
)
"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于任何 XML 文档(未使用)时,将计算 XPath 表达式并输出此计算结果:
2011-11-29 12:15:22
关于xml - 使用 XSLT 将 XML 中的日期和转换为 UTC 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669015/