在 XSLT 1.0 中格式化 24 小时时间的最佳方法是什么?
我很难找到采用时间格式并轻松确定其是否有效的好方法,然后使用 XSLT 1.0 生成具有某种格式的结果元素。
给定以下 xml:
<root>
<srcTime>2300</srcTime>
</root>
生成结果 xml 会很棒:
<root>
<dstTime>23:00</dstTime>
</root>
但是,如果源 xml 包含无效的 24 小时时间格式,则生成的 dstTime 元素应为空。
例如,当无效的源 xml 如下时:
<root>
<srcTime>NOON</srcTime>
</root>
生成的 xml 应该是:
<root>
<dstTime></dstTime>
</root>
问题是,可编写以产生所需结果的最佳 XSLT 1.0 片段是什么? 希望保持它非常简单,而不必每次都解析(即,如果可能的话,模式匹配会很甜蜜)。
I've had a hard time finding good ways of taking a time format and easily determining if it's valid then producing a resulting element that has some formatting using XSLT 1.0.
Given the following xml:
<root>
<srcTime>2300</srcTime>
</root>
It would be great to produce the resulting xml:
<root>
<dstTime>23:00</dstTime>
</root>
However, if the source xml contains an invalid 24 hour time format, the resulting dstTime element should be blank.
For example, when the invalid source xml is the following:
<root>
<srcTime>NOON</srcTime>
</root>
The resulting xml should be:
<root>
<dstTime></dstTime>
</root>
The question is, what's the best XSLT 1.0 fragment that could be written to produce the desired results? The hope would be to keep it quite simple and not have to parse the every piece of the time (i.e. pattern matching would be sweet if possible).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为了使列表完整,XSLT 中还有 日期/时间处理模块 部分Steve Ball 的标准库。
And to have the list complete, there is also Date/Time processing module part of XSLT Standard Library by Steve Ball.
即使是 exslt.org time() 函数在这里也无法为您提供帮助,因为它期望其输入采用正确的格式(xs:dateTime 或 xs:time)。
这是最好在 XSLT 之外解决的问题。 我这样说是因为我经常使用 XSLT 来做一些 XSLT 并非真正设计用途的事情,并且设法让事情正常工作。 它实际上并不是为解析字符串而设计的。
理想的解决方案是修复生成 XML 文档的任何内容,以便使用专门为此目的而方便建立的国际标准来格式化时间,使用的原则是,如果可以避免这样做,则不应保留或传输垃圾数据。
但如果这是不可能的,您应该在将数据传递到 XSLT 之前修复数据,或者在生成转换的输出之后修复它。
Even the exslt.org time() function won't help you here, because it expects its input to be in the proper format (xs:dateTime or xs:time).
This is something that is best fixed outside of XSLT. I say this as someone who routinely uses XSLT to do things it wasn't really designed for and manages to get things working. It was really not designed to parse strings.
The ideal solution is to fix whatever is producing the XML document so that it formats times using the international standard conveniently established just for that purpose, using the principle that you shouldn't persist or transmit crap data if you can avoid doing so.
But if that's not possible, you should either fix the data before passing it to XSLT or fix it after generating the transform's output.
根据实际使用的 xslt 处理器,您可以在自定义扩展函数中执行所需的操作(您必须自己创建)。
Xalan对扩展函数有很好的支持,你不仅可以用Java编写它们而且还可以使用 JavaScript 或 Apache BSF 支持的其他语言。
Microsoft 的 XSLT 引擎也支持自定义扩展,如 .NET Framework 开发人员指南中所述,扩展 XSLT 样式表
Depending on the actual xslt processor used you could be able to do desired operations in custom extension function (which you would have to make yourself).
Xalan has good support for extension functions, you can write them not only in Java but also in JavaScript or other languages supported by Apache BSF.
Microsoft's XSLT engine supports custom extensions as well, as described in .NET Framework Developer's Guide, Extending XSLT Style Sheets
看一下:
http://www.exslt.org/ 特别是“日期和时间”部分。
我还没有深入研究它,但看起来它可能就是您所寻找的。
Have a look at:
http://www.exslt.org/ specifically the "dates and times" section.
I haven't dug deep into it but it looks like it may be what your looking for.
XSLT 1.0 中没有任何正则表达式,因此恐怕无法进行模式匹配。
我不清楚
23:00
是否合法? 如果是,请尝试:如果不是,并且四位数字是唯一合法的,那么:
There aren't any regular expressions in XSLT 1.0, so I'm afraid that pattern matching isn't going to be possible.
I'm not clear if
<srcTime>23:00</srcTime>
is supposed to be legal or not? If it is, try:If it isn't, and four digits is the only thing that's legal then:
XSLT 1.0 没有任何对日期/时间操作的标准支持。
您必须编写一个简单的解析和格式化函数。 这不会很简单,也不会很美好。
XSLT 确实是为树转换而设计的。 这种文本节点操作最好在 XSLT 之外完成。
XSLT 1.0 does not have any standard support for date/time manipulation.
You must write a simple parsing and formatting function. That's not going to be simple, and that's not going to be pretty.
XSLT is really designed for tree transformations. This sort of text node manipulations are best done outside of XSLT.