XSLT date comparison

2019-08-03 07:13发布

Good morning, i have checked many responses about this topic, but without success...very sorry...

i have got an xml document with an element 'courses' ("seances") which has 'course' éléments ("seance") : (i have deleted unnecessary details)

....
<seances>
    <seance date="2014-09-10T00:00:01">
...details in a 'seance'
    </seance>
    <seance date="2013-09-10T00:00:01">
...
    </seance>
...other 'seance' elements
</seances>

and my xslt stylesheet produces an html document :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"   
    xmlns:date="http://exslt.org/dates-and-times" 
        extension-element-prefixes="date" 
    xmlns:gr="http://www.w3.org/2000/svg"  >

<xsl:output method="html" encoding="iso-8859-1" indent="yes"  />
<xsl:import href="date.xsl" />

to memorize the date of the day :

<xsl:variable name="ddj" as="xs:dateTime" select="date:date-time()"/>

i would like to show only an element course when the date of the course is ok (the date is not in the future) :

i have tried many conditions like this one but it is always false :

<xsl:template match="seance">
    <xsl:variable name="dateseance" as="xs:dateTime" select="@date"/>
    <xsl:value-of select="$dateseance" /><xsl:value-of select="$ddj" />
    <xsl:choose>
       <xsl:when test="$ddj  &gt;=  $dateseance">
          <xsl:text>-OK-</xsl:text>
       </xsl:when>
       <xsl:otherwise>
          <xsl:text>-Not OK-</xsl:text>
       </xsl:otherwise>
    </xsl:choose>   
 </xsl:template> 

Dates seems to be ok, but it prints :

2014-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- (will not be printed !)

other lines should be "ok" :

2013-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK- 2012-09-10T00:00:012013-09-11T10:00:00.004+02:00-Not OK-

i hope that it can be understood...

many thanks for any help.

A precision : i process it in mozilla/firefox

2条回答
看我几分像从前
2楼-- · 2019-08-03 07:19

There appears to be no problem when using the built-in facilities in XSLT 2.0 for date and time:

T:\ftemp>type dates.xml
<seances>
    <seance date="2014-09-10T00:00:01">
...details in a 'seance'
    </seance>
    <seance date="2013-09-10T00:00:01">
...
    </seance>
...other 'seance' elements
</seances>

T:\ftemp>xslt2 dates.xml dates.xsl
2014-09-10T00:00:012013-09-11T13:34:59.992-04:00-Not OK-
2013-09-10T00:00:012013-09-11T13:34:59.992-04:00-OK-

T:\ftemp>type dates.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
       exclude-result-prefixes="xs"
version="2.0"
    xmlns:gr="http://www.w3.org/2000/svg"  >

<xsl:output method="html" encoding="iso-8859-1" indent="yes"  />

<xsl:variable name="ddj" as="xs:dateTime" select="current-dateTime()"/>

<xsl:template match="seances">
  <xsl:apply-templates select="seance"/>
</xsl:template>

<xsl:template match="seance">
    <xsl:variable name="dateseance" as="xs:dateTime" select="@date"/>
    <xsl:value-of select="$dateseance" /><xsl:value-of select="$ddj" />
    <xsl:choose>
       <xsl:when test="$ddj  &gt;=  $dateseance">
          <xsl:text>-OK-</xsl:text>
       </xsl:when>
       <xsl:otherwise>
          <xsl:text>-Not OK-</xsl:text>
       </xsl:otherwise>
    </xsl:choose>
  <xsl:text>&#xa;</xsl:text>
 </xsl:template>

</xsl:stylesheet>
T:\ftemp>

Is there a reason you feel the need to use exslt?

查看更多
小情绪 Triste *
3楼-- · 2019-08-03 07:20

Below is an XSLT 1.0 solution that ignores time zones and assumes both date/time values are in the same time zone:

T:\ftemp>type dates.xml
<seances>
    <seance date="2014-09-10T00:00:01">
...details in a 'seance'
    </seance>
    <seance date="2013-09-10T00:00:01">
...
    </seance>
...other 'seance' elements
</seances>

T:\ftemp>xslt dates.xml dates.xsl
2014-09-10T00:00:01 2013-09-11T14:30:13 -Not OK-
2013-09-10T00:00:01 2013-09-11T14:30:13 -OK-

T:\ftemp>type dates.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes="date"
                version="1.0"
                xmlns:gr="http://www.w3.org/2000/svg"  >

<xsl:output method="html" encoding="iso-8859-1" indent="yes"  />

<xsl:variable name="ddj" select="substring(date:date-time(),1,19)"/>

<xsl:template match="seances">
  <xsl:apply-templates select="seance"/>
</xsl:template>

<xsl:template match="seance">
    <xsl:variable name="dateseance" select="@date"/>
    <xsl:value-of select="$dateseance" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$ddj" />
    <xsl:text> </xsl:text>
    <xsl:choose>
       <xsl:when test="translate($ddj,':T-','')  >=
                       translate($dateseance,':T-','')">
          <xsl:text>-OK-</xsl:text>
       </xsl:when>
       <xsl:otherwise>
          <xsl:text>-Not OK-</xsl:text>
       </xsl:otherwise>
    </xsl:choose>
  <xsl:text>&#xa;</xsl:text>
 </xsl:template>

</xsl:stylesheet>
T:\ftemp>

This works because it converts the date/time values into numbers which, in XSLT 1.0, can be compared using > and <.

查看更多
登录 后发表回答