2015-09-02 155 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

然后你的输入不能是你在原帖中显示的内容 –