日期减去 xslt 中的另一个日期
希望有人能帮忙。我试图比较 XML 文件中的 2 个日期并使用 XSLT 进行一些计算:
例如,我在 XML 中有 2 个日期:2011-05-23 和 2011-04-29。我想在 XSLT 中进行计算,如下所示:
('2011-05-23'-'2011-04-29')*30 = 24*30= 720
任何人都可以阐明吗?
Hope someone can help. I am trying to compare 2 dates inside an XML file and using a XSLT to do some calculation:
For example I have 2 dates in XML: 2011-05-23 and 2011-04-29. I want to do calculation inside XSLT like below:
('2011-05-23'-'2011-04-29')*30 = 24*30= 720
Can anyone shed any light?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XSLT 2.0 解决方案
产量:
720
xs:date()
函数计算日期,可用于执行xdt:dayTimeDuration
P24D
(24 天)days-from-duration()
从xdt:dayTimeDuration
(24)24*30=720
)An XSLT 2.0 solution
Yields:
720
xs:date()
function evaluates the dates, which can be used to perform date operationsxdt:dayTimeDuration
P24D
(24 days)days-from-duration()
extracts the days component from thexdt:dayTimeDuration
(24)24*30=720
)可能值得查看 EXSLT - date:difference 解决方案。我相信这应该可以满足您的要求,甚至可以直接使用 XSLT 实现。
请注意,返回的值采用 XML 架构第 2 部分中指定的持续时间格式:数据类型第二版,因此您可能需要处理结果以导出您希望在计算中使用的单位(例如,在上面的示例中,您期望得到一个详细说明天数差异的结果 - 因此您需要拿出您想要使用的相关单位,日期的结果:在这种情况下的差异很可能是“P24D”)。
It might be worth looking at the EXSLT - date:difference solution. I believe that should do what you want, and there's even a straight XSLT implementation available.
Be aware though that the returned value is in duration format as specified in the XML Schema Part 2: Datatypes Second Edition, so it's likely you will need to process the result to derive the unit you wish to use in calculation (for instance in your example above you're expecting a result detailing the number of days difference - so you would need to pull out the relevant unit you want to work with, the result from date:difference in that case would most likely be "P24D").
这是我有时用于日期计算的两个模板:
它们有点拗口,但它们会计算自 1970 年 1 月 1 日午夜以来的秒数/天数作为整数,然后您可以对其进行直接算术。它们依赖于
yyyy-mm-dd hh:mm:ss
的日期格式,但对子字符串调用的参数的操作应该允许您以所需的任何格式处理日期。Here's two templates I sometimes use for date calculations:
They're a bit of a mouthful, but they'll calculate the number of seconds/days since midnight 1st January 1970 as an integer, which you can then do straight arithmetic on. They rely on the date format being
yyyy-mm-dd hh:mm:ss
, but manipulation of the parameters of the substring calls should allow you to process dates in any format you need.