2015-05-19 294 views
0

我的XML包含时间字符串Wed Apr 22 02:49:26 2015使用xslt 1.0进行时间转换

<sample> 
<time>Wed Apr 22 02:49:26 2015</time> 
</sample> 

我需要减去从当前时间和显示这个时候就XSLT作为22:30:36

有没有办法做到这一点?感谢您能帮助我

+0

您正在使用哪种XSLT 1.0处理器?当差异超过24小时时,请澄清预期结果。 –

+0

抱歉,我无法回复。我的机器坠毁了。我正在使用javax处理器。实际上我需要计算我花了多长时间的持续时间。

+0

http://stackoverflow.com/help/someone-answers –

回答

0

XSLT 1.0没有日期或时间的概念,所以这很困难。即使单独获取当前日期或时间也是有问题的 - 尽管大多数XSLT 1.0处理器都支持用于此目的的扩展功能。

更优雅的解决方案很可能会写自己的扩展功能(在Java中)做这个任务 - 见:https://xml.apache.org/xalan-j/extensions.html#ext-functions

在这里,我将展示如何做到这一点纯粹的XSLT 1.0(与得到的例外当前日期和时间 - 也可以在运行时作为参数传递给样式表)。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:date="http://exslt.org/dates-and-times" 
extension-element-prefixes="date"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/sample"> 
    <duration> 
     <xsl:call-template name="elapsed-time"> 
      <xsl:with-param name="start" select="time" /> 
     </xsl:call-template>  
    </duration> 
</xsl:template> 

<xsl:template name="elapsed-time"> 
    <xsl:param name="start"/> 

    <xsl:variable name="mmm" select="substring($start, 5, 3)" /> 

    <xsl:variable name="JDN1"> 
     <xsl:call-template name="JDN"> 
      <xsl:with-param name="month" select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', $mmm)) div 3 + 1" /> 
      <xsl:with-param name="day" select="substring($start, 9, 2)" /> 
      <xsl:with-param name="year" select="substring($start, 21, 4)" /> 
      <xsl:with-param name="hour" select="substring($start, 12, 2)" /> 
      <xsl:with-param name="minute" select="substring($start, 15, 2)" /> 
      <xsl:with-param name="second" select="substring($start, 18, 2)" /> 
     </xsl:call-template> 
    </xsl:variable> 

    <xsl:variable name="today" select="date:date()" /> 
    <xsl:variable name="now" select="date:time()" /> 

    <xsl:variable name="JDN2"> 
     <xsl:call-template name="JDN"> 
      <xsl:with-param name="year" select="substring($today, 1, 4)" /> 
      <xsl:with-param name="month" select="substring($today, 6, 2)" /> 
      <xsl:with-param name="day" select="substring($today, 9, 2)" /> 
      <xsl:with-param name="hour" select="substring($now, 1, 2)" /> 
      <xsl:with-param name="minute" select="substring($now, 4, 2)" /> 
      <xsl:with-param name="second" select="substring($now, 7, 2)" /> 
     </xsl:call-template> 
    </xsl:variable> 

    <xsl:variable name="seconds" select="$JDN2 - $JDN1"/> 
    <xsl:variable name="h" select="floor($seconds div 3600)"/> 
    <xsl:variable name="r" select="$seconds mod 3600"/> 
    <xsl:variable name="m" select="floor($r div 60)"/> 
    <xsl:variable name="s" select="$r mod 60"/> 

    <xsl:value-of select="concat($h, format-number($m, ':00'), format-number($s, ':00'))"/> 
</xsl:template> 

<xsl:template name="JDN"> 
    <xsl:param name="year"/> 
    <xsl:param name="month"/> 
    <xsl:param name="day"/> 
    <xsl:param name="hour"/> 
    <xsl:param name="minute"/> 
    <xsl:param name="second"/> 

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/> 
    <xsl:variable name="y" select="$year + 4800 - $a"/> 
    <xsl:variable name="m" select="$month + 12*$a - 3"/>  
    <xsl:variable name="jd" select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" /> 

    <xsl:value-of select="86400*$jd + 3600*$hour + 60*$minute + $second" /> 
</xsl:template> 

</xsl:stylesheet> 

如果上述在十一时51分48秒施加到您的示例输入上2015年5月21日,其结果将是:

<?xml version="1.0" encoding="UTF-8"?> 
<duration>705:02:22</duration> 

备注:假设您的输入日期格式使用2位数字作为日期部分;否则你将需要使用不同的方法来解析它。