XSLT 中的秒到时间 [英] Seconds to Time in XSLT
问题描述
其实我不是在问问题.我注册了 stackoverflow,以回馈给我很多次帮助的社区.p>
如果您使用 XSLT 1.0,则没有内置时间计算.我想出了这个来将秒转换为分钟和小时.我希望这对某人有所帮助!
<xsl:param name="value"/><xsl:variable name="minutes" select="floor($value div 60) mod 60"/><xsl:variable name="seconds" select="$value mod 60"/><xsl:variable name="hours" select="floor($value div 3600)"/><xsl:if test="$hours"><xsl:if test="$hours < 10"><xsl:text>0</xsl:text></xsl:if><xsl:value-of select="$hours"/><xsl:text>:</xsl:text></xsl:if><xsl:if test="$minutes"><xsl:if test="$minutes < 10"><xsl:text>0</xsl:text></xsl:if><xsl:value-of select="$minutes"/><xsl:text></xsl:text></xsl:if><xsl:if test="$minutes and $seconds"><xsl:text>:</xsl:text></xsl:if><xsl:if test="$seconds"><xsl:value-of select="$seconds"/><xsl:text></xsl:text></xsl:if></xsl:模板>
<块引用>
您还有其他方法可以实现吗?
首先,感谢您的贡献.但是,这种实现存在一些问题.秒输出不考虑前导 0,但即使是固定的,如果任何数字恰好为 0,输出也可能不明确.例如,输入 36005
, 605
和 36300
都产生结果 10:05
.
同样,如果两个数字碰巧为零.输入 36000
产生输出 10:
,输入 600
和 10
都产生结果 10
.
以下是我将如何实现此功能:
<xsl:param name="value" select="."/><xsl:param name="alwaysIncludeHours" select="false()"/><xsl:param name="includeSeconds" select="true()"/><xsl:if test="$value > 3600 或 $alwaysIncludeHours"><xsl:value-of select="concat(format-number($value div 3600, '00'), ':')"/></xsl:if><xsl:value-of select="format-number(floor($value div 60) mod 60, '00')"/><xsl:if test="$includeSeconds"><xsl:value-of select="concat(':', format-number($value mod 60, '00'))"/></xsl:if></xsl:模板>
这样,小时仅在非零或可选打开时才显示,默认情况下包括秒,但可以选择使用参数值省略.这应该减少关于时间应该代表什么的歧义.
如果要格式化的值是当前上下文节点,它还允许省略 value
参数.
I'm not asking a question actually. I signed up stackoverflow to give something back to a community that has helped me so many times.
If you are using XSLT 1.0 there are no built-in time calculations. I came up with this to convert seconds into minutes and hours. I hope this helps someone!
<xsl:template name="format-duration">
<xsl:param name="value" />
<xsl:variable name="minutes" select="floor($value div 60) mod 60" />
<xsl:variable name="seconds" select="$value mod 60" />
<xsl:variable name="hours" select="floor($value div 3600)" />
<xsl:if test="$hours">
<xsl:if test="$hours < 10">
<xsl:text>0</xsl:text>
</xsl:if>
<xsl:value-of select="$hours" />
<xsl:text>:</xsl:text>
</xsl:if>
<xsl:if test="$minutes">
<xsl:if test="$minutes < 10">
<xsl:text>0</xsl:text>
</xsl:if>
<xsl:value-of select="$minutes" />
<xsl:text></xsl:text>
</xsl:if>
<xsl:if test="$minutes and $seconds">
<xsl:text>:</xsl:text>
</xsl:if>
<xsl:if test="$seconds">
<xsl:value-of select="$seconds" />
<xsl:text></xsl:text>
</xsl:if>
</xsl:template>
EDIT: Do you have any other methods to achieve the same?
Firt of all, thank you for your contribution. There are, however, a few issues with this implementation. The seconds output doesn't take the leading 0 into account, but even if that's fixed, the output can be ambiguous if any of the figures happens to be 0. For example, the inputs 36005
, 605
, and 36300
all produce the result 10:05
.
Likewise if two of the figures happen to be zero. The input 36000
produces the output 10:
, and the inputs 600
and 10
both produce the result 10
.
Here is how I would go about implementing this functionality:
<xsl:template name="format-duration">
<xsl:param name="value" select="." />
<xsl:param name="alwaysIncludeHours" select="false()" />
<xsl:param name="includeSeconds" select="true()" />
<xsl:if test="$value > 3600 or $alwaysIncludeHours">
<xsl:value-of select="concat(format-number($value div 3600, '00'), ':')"/>
</xsl:if>
<xsl:value-of select="format-number(floor($value div 60) mod 60, '00')" />
<xsl:if test="$includeSeconds">
<xsl:value-of select="concat(':', format-number($value mod 60, '00'))" />
</xsl:if>
</xsl:template>
This way, hours are only displayed if they are non-zero or optionally turned on, and seconds are included by default, but can be optionally omitted by using the parameter values. This should reduce the ambiguity about what a time is supposed to represent.
It also allows omitting the value
parameter if the value you want to format is the current context node.
这篇关于XSLT 中的秒到时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!