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.
我很难找到采用时间格式的好方法,并且很容易确定它是否有效,然后生成一个使用XSLT 1.0进行格式化的结果元素。
Given the following xml:
给出以下xml:
<root>
<srcTime>2300</srcTime>
</root>
It would be great to produce the resulting xml:
生成生成的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.
但是,如果源xml包含无效的24小时时间格式,则生成的dstTime元素应为空。
For example, when the invalid source xml is the following:
例如,当无效的源xml如下:
<root>
<srcTime>NOON</srcTime>
</root>
The resulting xml should be:
生成的xml应为:
<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).
问题是,什么是可以编写以产生所需结果的最佳XSLT 1.0片段?希望是保持它非常简单,而不必解析每一段时间(即如果可能,模式匹配将是甜蜜的)。
6 个解决方案
#1
5
There aren't any regular expressions in XSLT 1.0, so I'm afraid that pattern matching isn't going to be possible.
XSLT 1.0中没有任何正则表达式,所以我担心模式匹配是不可能的。
I'm not clear if <srcTime>23:00</srcTime>
is supposed to be legal or not? If it is, try:
我不清楚
<dstTime>
<xsl:if test="string-length(srcTime) = 4 or
string-length(srcTime) = 5">
<xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
<xsl:if test="$hour >= 0 and 24 > $hour">
<xsl:variable name="minute">
<xsl:choose>
<xsl:when test="string-length(srcTime) = 5 and
substring(srcTime, 3, 1) = ':'">
<xsl:value-of select="substring(srcTime, 4, 2)" />
</xsl:when>
<xsl:when test="string-length(srcTime) = 4">
<xsl:value-of select="substring(srcTime, 3, 2)" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:if test="$minute >= 0 and 60 > $minute">
<xsl:value-of select="concat($hour, ':', $minute)" />
</xsl:if>
</xsl:if>
</xsl:if>
</dstTime>
If it isn't, and four digits is the only thing that's legal then:
如果不是,那么四位数是唯一合法的东西:
<dstTime>
<xsl:if test="string-length(srcTime) = 4">
<xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
<xsl:if test="$hour >= 0 and 24 > $hour">
<xsl:variable name="minute" select="substring(srcTime, 3, 2)" />
<xsl:if test="$minute >= 0 and 60 > $minute">
<xsl:value-of select="concat($hour, ':', $minute)" />
</xsl:if>
</xsl:if>
</xsl:if>
</dstTime>
#2
3
XSLT 1.0 does not have any standard support for date/time manipulation.
XSLT 1.0没有任何标准的日期/时间操作支持。
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.
XSLT实际上是为树转换而设计的。这种文本节点操作最好在XSLT之外完成。
#3
2
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).
根据所使用的实际xslt处理器,您可以在自定义扩展功能(您必须自己制作)中执行所需的操作。
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.
Xalan对扩展函数有很好的支持,你不仅可以用Java编写,也可以用JavaScript或Apache BSF支持的其他语言编写。
Microsoft's XSLT engine supports custom extensions as well, as described in .NET Framework Developer's Guide, Extending XSLT Style Sheets
Microsoft的XSLT引擎也支持自定义扩展,如.NET Framework开发人员指南中所述,扩展XSLT样式表
#4
1
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.
请查看:http://www.exslt.org/,特别是“日期和时间”部分。我没有深入挖掘它,但看起来它可能是你想要的。
#5
1
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).
甚至exslt.org time()函数也不会帮助你,因为它希望它的输入格式正确(xs:dateTime或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.
这是最好在XSLT之外修复的东西。我说这是一个经常使用XSLT来做事情的人,这些事情并不是真正意义上的,而是设法使事情发挥作用。它实际上不是为解析字符串而设计的。
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.
理想的解决方案是修复生成XML文档的任何内容,以便使用为此目的而方便地建立的国际标准来格式化时间,使用不应该保留或传输垃圾数据的原则(如果您可以避免这样做)。
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之前修复数据,或者在生成转换输出后修复它。
#6
1
And to have the list complete, there is also Date/Time processing module part of XSLT Standard Library by Steve Ball.
为了完成列表,Steve Ball还提供了XSLT标准库的日期/时间处理模块部分。
#1
5
There aren't any regular expressions in XSLT 1.0, so I'm afraid that pattern matching isn't going to be possible.
XSLT 1.0中没有任何正则表达式,所以我担心模式匹配是不可能的。
I'm not clear if <srcTime>23:00</srcTime>
is supposed to be legal or not? If it is, try:
我不清楚
<dstTime>
<xsl:if test="string-length(srcTime) = 4 or
string-length(srcTime) = 5">
<xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
<xsl:if test="$hour >= 0 and 24 > $hour">
<xsl:variable name="minute">
<xsl:choose>
<xsl:when test="string-length(srcTime) = 5 and
substring(srcTime, 3, 1) = ':'">
<xsl:value-of select="substring(srcTime, 4, 2)" />
</xsl:when>
<xsl:when test="string-length(srcTime) = 4">
<xsl:value-of select="substring(srcTime, 3, 2)" />
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:if test="$minute >= 0 and 60 > $minute">
<xsl:value-of select="concat($hour, ':', $minute)" />
</xsl:if>
</xsl:if>
</xsl:if>
</dstTime>
If it isn't, and four digits is the only thing that's legal then:
如果不是,那么四位数是唯一合法的东西:
<dstTime>
<xsl:if test="string-length(srcTime) = 4">
<xsl:variable name="hour" select="substring(srcTime, 1, 2)" />
<xsl:if test="$hour >= 0 and 24 > $hour">
<xsl:variable name="minute" select="substring(srcTime, 3, 2)" />
<xsl:if test="$minute >= 0 and 60 > $minute">
<xsl:value-of select="concat($hour, ':', $minute)" />
</xsl:if>
</xsl:if>
</xsl:if>
</dstTime>
#2
3
XSLT 1.0 does not have any standard support for date/time manipulation.
XSLT 1.0没有任何标准的日期/时间操作支持。
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.
XSLT实际上是为树转换而设计的。这种文本节点操作最好在XSLT之外完成。
#3
2
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).
根据所使用的实际xslt处理器,您可以在自定义扩展功能(您必须自己制作)中执行所需的操作。
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.
Xalan对扩展函数有很好的支持,你不仅可以用Java编写,也可以用JavaScript或Apache BSF支持的其他语言编写。
Microsoft's XSLT engine supports custom extensions as well, as described in .NET Framework Developer's Guide, Extending XSLT Style Sheets
Microsoft的XSLT引擎也支持自定义扩展,如.NET Framework开发人员指南中所述,扩展XSLT样式表
#4
1
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.
请查看:http://www.exslt.org/,特别是“日期和时间”部分。我没有深入挖掘它,但看起来它可能是你想要的。
#5
1
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).
甚至exslt.org time()函数也不会帮助你,因为它希望它的输入格式正确(xs:dateTime或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.
这是最好在XSLT之外修复的东西。我说这是一个经常使用XSLT来做事情的人,这些事情并不是真正意义上的,而是设法使事情发挥作用。它实际上不是为解析字符串而设计的。
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.
理想的解决方案是修复生成XML文档的任何内容,以便使用为此目的而方便地建立的国际标准来格式化时间,使用不应该保留或传输垃圾数据的原则(如果您可以避免这样做)。
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之前修复数据,或者在生成转换输出后修复它。
#6
1
And to have the list complete, there is also Date/Time processing module part of XSLT Standard Library by Steve Ball.
为了完成列表,Steve Ball还提供了XSLT标准库的日期/时间处理模块部分。