2015-09-02 154 views
0

我有這樣XSL:如何比較兩個日期?

<event> 
    <name>Jazz festival</name> 
    <place>Rome</place> 
    <date>23/06/2014</date> 
</event> 

我要檢查,如果日期之前或之後再2014年10月1日通過XSL的XML。 任何人都可以幫助我嗎?

+2

當涉及到日期進行比較,這是至關重要的,何況你是否使用XSLT 1.0(意味着:手工做)或XSLT 2.0(意味着:轉換到'xs:date'並且簡單地使用比較運算符)。請更新你的問題,並且如Andy所說,顯示你到目前爲止的問題:[問]。 – Abel

回答

3

試試這樣說:

XSLT 1.0

<xsl:template match="event"> 
    <xsl:variable name="date" select="10000 * substring(date, 7, 4) + 100 * substring(date, 4, 2) + substring(date, 1, 2)"/> 
    <xsl:choose> 
     <xsl:when test="$date > 20141001 "> 
      <!-- code for dates later than 2014-10-01 --> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- code for dates earlier than or equal to 2014-10-01 --> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

XSLT 2.0

<xsl:template match="event"> 
    <xsl:variable name="date" select="xs:date(concat(substring(date, 7, 4), '-', substring(date, 4, 2), '-', substring(date, 1, 2)))"/> 
    <xsl:choose> 
     <xsl:when test="$date gt xs:date('2014-10-01')"> 
      <!-- code for dates later than 2014-10-01 --> 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- code for dates earlier than or equal to 2014-10-01 --> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
1

謝謝。最後,這是我做的

<xsl:variable name="data" select="concat(substring-after(substring-after(.,'/'),'/') , format-number(substring-before(substring-after(.,'/'),'/'), '00') , format-number(number(substring-before(.,'/')), '00'))"/> 
<xsl:choose> 
    <xsl:when test="$data &gt; '20151018000000'"> 
     ... 
    </xsl:when> 
    <xsl:otherwise> 
      ... 
    </xsl:otherwise> 
</xsl:choose> 
+0

** 1。**原始日期是否已經填充到固定長度?你的例子顯示至少在這個月份是。 - ** 2。**我認爲你需要等很長的時間才能得到'$ data >'20151018000000''會返回true ... –

+0

不,不,我試過了,效果不錯 –

+0

然後你的輸入不能是你在原帖中顯示的內容 –