xml 在 XSLT 中将字符串转换为日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16892344/
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
Convert a string to Date format in XSLT
提问by Rg90
I have a date(string) value in an XML file in this format:
我在这种格式的 XML 文件中有一个日期(字符串)值:
Tue Apr 17 03:12:47 IST 2012
I want to use XSL transformation to convert the string/date into this format:
我想使用 XSL 转换将字符串/日期转换为这种格式:
4/17/2012 03:12:47 AM
How can I do that in my XSL transform?
我怎样才能在我的 XSL 转换中做到这一点?
回答by Siva Charan
If you are using
如果您正在使用
XSLT 1.0 version, use EXSLT - date:format-datedate extension
XSLT 2.0 version, use built-in: Formatting Dates and Timesdate extension
XSLT 1.0 版本,使用EXSLT - date:format-date日期扩展
XSLT 2.0 版本,使用内置:Formatting Dates and Times日期扩展
But my suggestion is to
但我的建议是
Have a standard XSD datetime format on XML, on the code-behind (that is, on rendering time) you can format as you like.
在 XML 上有一个标准的 XSD 日期时间格式,在代码隐藏(即渲染时间)上,您可以随意格式化。
Update:
更新:
Always XML to process through XSLT, dates should be in standard XSD format. Currently your input is not in standard format so that it throws error.
始终通过 XSLT 处理 XML,日期应采用标准 XSD 格式。目前您的输入不是标准格式,因此会引发错误。
Example:
例子:
<xsl:variable name="dt" as="xs:dateTime" select="xs:dateTime('2012-10-21T22:10:15')"/>
<xsl:value-of select="format-dateTime($dt, '[Y0001]/[M01]/[D01]')"/>
OUTPUT:
输出:
2012/10/21
2012/10/21

