2013-08-20 80 views
3

我想實現一個格式規則,其中我有一個日期輸入字符串「1918-12-31T24:00:00Z」,我需要顯示它爲「1/1/1918" 年。代碼如下:無法把邏輯格式化日期

請注意,價值相關=「1918-12-31T24:00:00Z」或類似的格式。

<xsl:choose> 
      <xsl:when test="not($relement)"/> 
      <xsl:when test="$relement = ''"/> 
      <xsl:otherwise> 
       <xsl:variable name="rdate" select="substring-before('$relement','T')"/> 
       <xsl:variable name="month" select="substring-before(substring-after($rdate,'-'),'-')"/>          
       <xsl:variable name="day" select="substring-after(substring-after($rdate,'-'),'-')"/> 
       <xsl:variable name="year" select="substring-before($rdate,'-')"/> 
       <xsl:variable name="time" select="substring-after('$relement','T')"/>     

       <xsl:variable name="lastdate"> 
       <xsl:choose> 
       <xsl:when test="$month = 2 and not($year mod 4) and ($year mod 100 or not($year mod 400))"> 
        <xsl:value-of select="29"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="substring('312831303130313130313031', 2 * $month - 1,2)"/> 
       </xsl:otherwise> 
       </xsl:choose> 
       </xsl:variable> 

       <xsl:variable name="dayadj"> 
       <xsl:choose> 
       <xsl:when test="contains($time,'24:00:00Z')"> 2011-12-31T24:00:00Z 
        <xsl:value-of select="$day + '1'"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="$day"/> 
       </xsl:otherwise> 
       </xsl:choose> 
       </xsl:variable> 
       <xsl:variable name="finaldate"> 
       <xsl:choose> 
       <xsl:when test="$dayadj > $lastdate and ($month = 12)"> 
        <xsl:value-of select="concat('1','/','1','/',$year + '1')"/> 
       </xsl:when> 
       <xsl:when test="$dayadj > $lastdate and ($month != 12)"> 
        <xsl:value-of select="concat($month + '1','/','1','/',$year)"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="concat($month,'/',$dayadj,'/',$year)"/> 
       </xsl:otherwise> 
       </xsl:choose> 
       </xsl:variable> 
      <xsl:attribute name="internal"><xsl:value-of select="$arg1/@internal"/></xsl:attribute> 
       <xsl:value-of select="$finaldate"/> 
     </xsl:otherwise> 
</xsl:choose> 

上面的代碼失敗在給輸出,因爲我無法找到應用驗證在該月的最後一天,一年以及邏輯。

我得到的錯誤輸出是:「////」在所有情況下。

+0

(at)Filburt很好的編輯;) – hek2mgl

+0

是否有可能在塊中包含一些邏輯? – apratik

+0

請參閱http://soajagat.blogspot.com/2009/06/xpath-10-datetime-nightmares.html – fred02138

回答

0

兩個問題:

1)有,你指的是$relement變量兩個地方,但你有它引述:'$relement'。這是使用文字字符串「$relement」而不是引用該變量。

2)你必須在該行的末尾看似隨意日期2011-12-31T24:00:00Z開始<xsl:when test="contains($time,'24:00:00Z')">

看到這個XSLTCake演示的工作版本。