XSLT中的时间 [英] Seconds to Time in XSLT

查看:123
本文介绍了XSLT中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是问一个问题。我注册了stackoverflow来给一些帮助我很多次的社区。

如果你使用的是XSLT 1.0,那么没有内置的时间计算。我想出了这个将秒数转换成几分钟和几个小时。我希望这有助于某人!

 < xsl:template name =format-duration> 
< xsl:param name =value/>

< xsl:variable name =minutesselect =floor($ value div 60)mod 60/>
< xsl:variable name =secondsselect =$ value mod 60/>
< xsl:variable name =hoursselect =floor($ value div 3600)/>


< xsl:if test =$ hours>
< xsl:if test =$ hours& lt; 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& lt; 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>




编辑:
你有什么其他方法可以实现相同?



解决方案

所有的谢谢,谢谢你的贡献。但是,这个实现有一些问题。秒输出不考虑前导0,但即使这是固定的,如果任何数字恰好为0,输出可能是模糊的。例如,输入 36005 605 36300 都产生结果 10:05



同样,如果两个数字恰好为零。输入 36000 产生输出 10:,输入 600 10 都产生结果 10



这是我如何实现这个功能:

 < xsl:template name =format-duration> 
< xsl:param name =valueselect =。 />
< xsl:param name =alwaysIncludeHoursselect =false()/>
< xsl:param name =includeSecondsselect =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>

这样,只有在非零或可选开启时才会显示小时数,秒数默认包含,但可以通过使用参数值可选地省略。这应该减少关于什么时候应该代表的歧义。



如果要格式化的值是当前上下文节点,它还可以省略参数。 p>

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 &lt; 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 &lt; 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆