XSLT 1.0 Get Current DateTime

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I have a node in my XML file containing the following:

2011-12-01T16:33:33Z

I wish to take this line and replace it with the current date and time using the same format as shown above.

YYYY-MM-DDTHH:MM:SSZ

The node is within a namespace declared as 'x'

回答1:

Playing with DateTime is not possible with XSLT 1.0 alone .. In a similar situations I took help of scripting .. (C#)

Sample XML:

2011-12-01T16:33:33Z

Sample XSLT:

Resulting Output:

2012-02-22T18:03:12Z

The script may reside in a same file (like I have it in my sample XSLT code) or if the code triggering XSLTransformation is C# then move the same code in the calling place :)



回答2:

It's better to pass current datetime from your XML engine. Declare in your xsl:stylesheet, and pass the value from processor.



回答3:

You'll better pass the current data as an input / xsl:param to the template.

The XSLT aims to be purely functional language; that is, all templates / functions should conform to e.g. the following condition: If a pure function is called with parameters that cause no side-effects, the result is constant with respect to that parameter list (sometimes called referential transparency), i.e. if the pure function is again called with the same parameters, the same result will be returned (this can enable caching optimizations such as memoization).

Although there are workarounds on this (as InfantPro'Aravind' pointed out), it is not recommended to do such things; by doing it, you're ruining one of the most significant XSLT benefits.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!