如何在XSLT中将current-dateTime生成的次日时间固定为00:01:00?
解决方法
别用字符串替换来改时间,xs:dateTime是结构化的日期时间类型,直接操作字符串容易因格式差异(比如时区、毫秒)出错。推荐用XSLT原生的日期时间函数构造目标值:
- 先获取次日的日期:用
current-date()加1天,得到纯日期类型结果 - 将这个日期和固定时间
00:01:00合并为xs:dateTime类型
具体代码可以这么写:
<!-- 一步到位的简洁写法 --> <xsl:value-of select="dateTime(current-date() + xs:dayTimeDuration('P1D'), xs:time('00:01:00'))"/> <!-- 拆分变量的写法,逻辑更清晰 --> <xsl:variable name="next-day" select="current-date() + xs:dayTimeDuration('P1D')"/> <xsl:variable name="fixed-time" select="xs:time('00:01:00')"/> <xsl:value-of select="dateTime($next-day, $fixed-time)"/>
为什么replace无效?
你之前用replace失败,是因为current-dateTime()+xs:dayTimeDuration('P1D')返回的是xs:dateTime对象,不是字符串。就算转成字符串再替换也不推荐——比如原日期带毫秒(如2024-05-20T14:30:00.123+08:00),或者时区格式有差异,替换逻辑很容易出错,维护性极差。
内容的提问来源于stack exchange,提问作者Dharmendra Kumar
相关产品推荐
相关产品推荐

