如何在XSLT中从xs:duration提取毫秒值?
在XSLT中提取xs:duration的小数秒/毫秒值
问题原因分析
seconds-from-duration()返回的是整数类型的秒数,你的PT0.0032S只有0.0032秒,整数部分为0,所以返回0是符合函数定义的。format-dateTime()要求传入xs:dateTime类型参数,而xs:duration是完全不同的类型,类型不匹配导致编译错误是正常现象。
解决方案
XSLT 2.0及以上版本(推荐)
利用XSLT 2.0支持的duration算术运算,直接将duration转换为秒数(含小数):
<!-- 假设$dur是你的xs:duration变量 --> <xsl:variable name="dur" select="xs:duration('PT0.0032S')"/> <!-- 获取精确秒数 --> <xsl:value-of select="$dur div xs:dayTimeDuration('PT1S')"/> <!-- 输出:0.0032 --> <!-- 转换为毫秒 --> <xsl:value-of select="$dur div xs:dayTimeDuration('PT1S') * 1000"/> <!-- 输出:3.2 -->
XSLT 1.0版本(兼容旧环境)
由于XSLT 1.0不支持duration的算术运算,需要通过字符串处理提取秒数部分:
<xsl:variable name="dur-str" select="'PT0.0032S'"/> <!-- 提取秒数字符串 --> <xsl:variable name="seconds-value" select="substring-before(substring-after($dur-str, 'PT'), 'S')"/> <!-- 获取精确秒数 --> <xsl:value-of select="$seconds-value"/> <!-- 输出:0.0032 --> <!-- 转换为毫秒 --> <xsl:value-of select="$seconds-value * 1000"/> <!-- 输出:3.2 -->
内容的提问来源于stack exchange,提问作者synnek1337
相关产品推荐
相关产品推荐

