Exclude items who has a later date than the date o

2019-07-19 00:25发布

问题:

I have a question about XSLT.

On a website i have a simple calendar, showing events who are going on in the future.

But when an event is done, it should be removed from my list. By done i mean, that the date of the event is past the date of today.

Every event have a date attached.

Look at the folowing code:

<xsl:for-each select="$currentPage//node[@template='1238']">

    <xsl:sort data-type="text" select="./data[@alias='dato']" order="ascending"/>

    <div class="aktivitet_forside">
            ...MY EVENT CONTENT...                    
    </div>

</xsl:for-each>

The above code will show me all events with a template='1238' and that is also correct, because that is the template of my events.

But as I mentioned before every event have a field containing a date. The field is called "dato". This is also the number I am sorting the events after, so the event with a date closest to today will be shown at the top of the list.

The format of the "dato"-field is: 2009-08-30T08:59:00

How do i automaticaly remove an event where the value of the "date"-field is past the date of today?

So if todays date is: 3th of september 2009, an event who has a date of 30th of august 2009 should not figure on the list.

回答1:

Assuming that you are using XSLT 1.0, If your XSLT processor supports the EXSLT extensions, you could make use of the date:difference function. There is also a plain XSLT template that is available which you can adapt to your scenario in case you do not have EXSLT natively available.

http://www.exslt.org/date/functions/difference/index.html

EDIT:

There could be another simpler version where you convert your date-time-string to become a number having the format YYYYMMDDHHMMSS. This will be ordered in numerically ascending order automatically(future dates/times will have a bigger number than previous dates), you could then take the current day's date-time string in the same way and do a regular numeric difference to see if the day is after or before the current date.

<xsl:variable name="newFormat" select="translate('2009-08-30T08:59:00', '-T:', '')"/>

Gives 20090830085900

In your XPath, you can do this, assuming $currDateTime is set to the right translated value and the <data> node has the date to check, you could use the below..

<xsl:for-each select="$currentPage//node[@template='1238'][data[@alias='dato' and ((number(translate(. ,'-T:', '')) - number($now)) &gt;=0)]]">


标签: date xslt