xml XSLT 1.0 获取当前日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9394322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT 1.0 Get Current DateTime
提问by Mike
I have a node in my XML file containing the following:
我的 XML 文件中有一个节点,其中包含以下内容:
<Apple>2011-12-01T16:33:33Z</Apple>
<Apple>2011-12-01T16:33:33Z</Apple>
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
YYYY-MM-DDTHH:MM:SSZ
The node is within a namespace declared as 'x'
该节点位于声明为“x”的命名空间内
采纳答案by InfantPro'Aravind'
Playing with DateTime is not possible with XSLT 1.0 alone .. In a similar situations I took help of scripting .. (C#)
单独使用 XSLT 1.0 无法使用 DateTime .. 在类似的情况下,我求助于脚本编写 .. (C#)
Sample XML:
示例 XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<Apple>2011-12-01T16:33:33Z</Apple>
</root>
Sample XSLT:
示例 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:cs="urn:cs">
<xsl:output method="xml" indent="yes"/>
<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
public string datenow()
{
return(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"));
}
]]>
</msxsl:script>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Apple">
<xsl:copy>
<xsl:value-of select="cs:datenow()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Resulting Output:
结果输出:
<?xml version="1.0" encoding="utf-8"?>
<root>
<Apple>2012-02-22T18:03:12Z</Apple>
</root>
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 :)
脚本可能驻留在同一个文件中(就像我在我的示例 XSLT 代码中那样)或者如果触发 XSLTransformation 的代码是 C#,则在调用位置移动相同的代码:)
回答by Kirill Polishchuk
It's better to pass current datetime from your XML engine. Declare <xsl:param name="current-datetime"/>in your xsl:stylesheet, and pass the value from processor.
最好从您的 XML 引擎传递当前日期时间。<xsl:param name="current-datetime"/>在您的 中声明xsl:stylesheet,并从处理器传递值。
回答by penartur
You'll better pass the current data as an input / xsl:param to the template.
您最好将当前数据作为输入 / xsl:param 传递给模板。
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).
XSLT 旨在成为纯粹的函数式语言;也就是说,所有模板/函数都应该符合例如以下条件:如果使用不会引起副作用的参数调用纯函数,则结果相对于该参数列表是恒定的(有时称为引用透明度),即如果再次使用相同的参数调用纯函数,将返回相同的结果(这可以启用缓存优化,例如记忆)。
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.
尽管对此有解决方法(如InfantPro'Aravind' 所指出的),但不建议这样做;这样做会破坏 XSLT 最重要的好处之一。

