views:

215

answers:

1

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.

+2  A: 

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)]]">
Thiyagaraj
What is that EXSLT? I don't think that I have it available.Isn't there a way to do it without it?
Kim Andersen
While it would be a little time consuming to write a date difference function on your own(You have to figure out leap years, no. of days etc.) - that link I provided has a normal XSLT Template which you can include in your stylesheet, you could take a look and adapt it to your problem.
Thiyagaraj
Updated a simpler solution - But this is assuming that you can get the current date-time in your XSLT stylesheet somehow, either in the input document or otherwise.
Thiyagaraj
Yeah I think that this could probably work, but how do I make a regular numeric difference?If I have my current date i a varable called $now in the same format as your $newFormat.
Kim Andersen
Updated answer to add this to your stylesheet - please check it out and let me know.
Thiyagaraj
I used your select in my xsl:for-each instead of the xsl:sort so it now looks like this:<xsl:for-each select="$currentPage//node[@template='1238'][data[@alias='dato' and ((number(translate(. ,'-T:', '')) - number($now)) >=0)]]">And that works :) 1+ to you and thank you very much for your help
Kim Andersen
Ok, I have updated the answer to include that in the for-each (didnt notice that previoulsy, copy pasted), Thanks - have a great day!
Thiyagaraj