xml - XSLT:UTC TimeStamp 使用adjust-dateTime-to-timezone 转换为时区
问题描述
我正在尝试将基于 XML 中的某些时区信息的 UTC 时间戳转换为我的目标 TZ 相关时间戳。
使用 xslt 函数adjust-dateTime-to-timezone($utc-timestamp, xs:dayTimeDuration('PT2H')
这不是问题,但转换必须是基于源文件(主要是 Java TZ)中的 TZ 信息的动态转换。
只是一个有效但不是真正动态的例子:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/InputDateTime">
<xs:testDate>
<xsl:variable name="targetTimeZone" select="TargetTimeZone/node()"/>
<xsl:variable name="utc-timestamp" select="UTCDateTime"/>
<xsl:variable name="xslTimeZone">
<xsl:choose>
<xsl:when test="$targetTimeZone = 'CET'">PT2H</xsl:when>
<xsl:otherwise>Not_Supported</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="tz-timestamp"
select="adjust-dateTime-to-timezone($utc-timestamp, xs:dayTimeDuration($xslTimeZone))"/>
<date UTC_timestamp="{$utc-timestamp}">
<cet CET_timestamp="{$tz-timestamp}">
<xsl:value-of
select="format-dateTime($tz-timestamp, '[M,2]/[D,2]/[Y,4] [h]:[m]:[s] [P]', 'de', (), 'de')"
/>
</cet>
</date>
</xs:testDate>
</xsl:template>
</xsl:stylesheet>
以这个输入 XML 文件为例:
<?xml version="1.0" encoding="UTF-8"?>
<InputDateTime>
<TargetTimeZone>CET</TargetTimeZone>
<UTCDateTime>2019-11-15T09:15:00Z</UTCDateTime>
</InputDateTime>
并产生正确的输出:
<?xml version="1.0" encoding="UTF-8"?>
<xs:testDate xmlns:xs="http://www.w3.org/2001/XMLSchema">
<date UTC_timestamp="2019-11-15T09:15:00Z">
<cet CET_timestamp="2019-11-15T11:15:00+02:00">11/15/2019 11:15:00 a.m.</cet>
</date>
</xs:testDate>
任何人都可以帮助我如何从像“CET”这样的任何 Java 时区转换为像“PT2H”这样的 xslt 持续时间,而无需手动维护所有这些?
谢谢和问候马可
解决方案
有像http://worldtimeapi.org/这样的 REST API ,它允许您请求有关时区的信息,所以
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
expand-text="yes"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="tz-api" as="xs:string">http://worldtimeapi.org/api/timezone/</xsl:param>
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="InputDateTime">
<date UTC_timestamp="{UTCDateTime}">
<xsl:variable name="tz-map" select="json-doc($tz-api || TargetTimeZone)"/>
<xsl:variable name="tz-dateTime" select="adjust-dateTime-to-timezone(xs:dateTime(UTCDateTime), timezone-from-dateTime(xs:dateTime($tz-map?datetime)))"/>
<local_date timestamp="{$tz-dateTime}">{format-dateTime($tz-dateTime, '[M,2]/[D,2]/[Y,4] [h]:[m]:[s] [P]', 'de', (), 'de')}</local_date>
</date>
</xsl:template>
</xsl:stylesheet>
在https://xsltfiddle.liberty-development.net/93dFK9G输出
<date UTC_timestamp="2019-11-15T09:15:00Z">
<local_date timestamp="2019-11-15T11:15:00+02:00">[Language: en]11/15/2019 11:15:00 a.m.</local_date>
</date>
如果 REST API 未找到时区缩写,我没有检查有关返回的错误的任何详细信息,但我确信 XSLT 3 可以在需要时使用xsl:try
/处理它。xsl:catch
推荐阅读
- java - 杰克逊 JSON 表示问题
- excel - 在 VBA 中,用文本值替换公式输出
- macos - 查找 Active Directory 的结构
- python - 在 def 函数中打印 txt 文件
- sql-server - What-IF 视图比较器的 UNION NULLS 后续行动
- internet-explorer - IE Dialog Modal 窗口上的 JSF 命令按钮
- php - 您可以从 PHP 上传中获取图像吗?
- c++ - 从字符串中获取 '\n'
- c# - Xamarin:无法解析程序集:“Google.Apis.Core”
- ios - 呈现ViewController 不工作