2015-05-19 115 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位數字作爲日期部分;否則你將需要使用不同的方法來解析它。