My XSLT skills are limited and I think I need help on this one. I basically need to display a particular message depending on the time of day. I could do this quite easily in C# but I like to make my life difficult so I am using XSLT because I need to develop my skills in this area and a user control is probably overkill for this anyway.
I don't think the comparison in the following example is not actually happening, perhaps XSLT can only compare simple data types such as strings and numbers and not dates?
<xsl:choose>
<xsl:when test="$currentTime > Exslt.ExsltDatesAndTimes:time('12:00:00') and $currentTime < Exslt.ExsltDatesAndTimes:time('17:00:00')">
Good Afternoon
</xsl:when>
<xsl:when test="$currentTime > Exslt.ExsltDatesAndTimes:time('17:00:00') and $currentTime < Exslt.ExsltDatesAndTimes:time('00:00:00')">
Good evening
</xsl:when>
<xsl:otherwise>
Good Morning
</xsl:otherwise>
</xsl:choose>
Any advice on where I am going wrong or a pointer to a better solution would be appreciated.
Thanks Matt, it works with the exception of midnight because that translates to 000000 so I had to fudge that to 235959 but hey what's 1 second really. It feels a little dirty but will do as a quick solution for the time being.
In case anyone else wants something like this, here is my complete solution:
You could use DateGreaterThan or DateGreaterThanOrEqual (I wouldn't use DateGreaterThanToday, as it only does a day based comparison, not time which simon is after), but I didn't go that route as it sounded like he wanted a pure XSLT suggestion, but yea, definatley one to concider if you are wanting to do the same.
Hi Matt, sure - good point, if you need the time as well you have to use one of the other functions. But I would at any time prefer one of the standard extension methods over trying to parse the number yourself, relying on specific date/time format etc.
I think in this case where the time is not an input it is probably ok but if the time comparison was a value being passed in then I agree it's not really a safe option.
Time comparison with XSLT
My XSLT skills are limited and I think I need help on this one. I basically need to display a particular message depending on the time of day. I could do this quite easily in C# but I like to make my life difficult so I am using XSLT because I need to develop my skills in this area and a user control is probably overkill for this anyway.
I don't think the comparison in the following example is not actually happening, perhaps XSLT can only compare simple data types such as strings and numbers and not dates?
<xsl:choose> <xsl:when test="$currentTime > Exslt.ExsltDatesAndTimes:time('12:00:00') and $currentTime < Exslt.ExsltDatesAndTimes:time('17:00:00')"> Good Afternoon </xsl:when> <xsl:when test="$currentTime > Exslt.ExsltDatesAndTimes:time('17:00:00') and $currentTime < Exslt.ExsltDatesAndTimes:time('00:00:00')"> Good evening </xsl:when> <xsl:otherwise> Good Morning </xsl:otherwise> </xsl:choose>Any advice on where I am going wrong or a pointer to a better solution would be appreciated.
You could convert the date into a number, then do a simple comparison on that? Take a look at an example here:
http://kodethoughts.blogspot.com/2007/10/sharepoint-comparing-dates-in-xslt.html
Matt
Thanks Matt, it works with the exception of midnight because that translates to 000000 so I had to fudge that to 235959 but hey what's 1 second really. It feels a little dirty but will do as a quick solution for the time being.
In case anyone else wants something like this, here is my complete solution:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt" xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary "> <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:param name="currentPage"/> <xsl:variable name="currentTime" select="number(translate(Exslt.ExsltDatesAndTimes:time(),':',''))"/> <xsl:variable name="midday" select="120000"/> <xsl:variable name="evening" select="170000"/> <xsl:variable name="midnight" select="235959"/> <xsl:template match="/"> <xsl:choose> <xsl:when test="$currentTime > $midday and $currentTime < $evening"> Good Afternoon </xsl:when> <xsl:when test="$currentTime > $evening and $currentTime < $midnight"> Good evening </xsl:when> <xsl:otherwise> Good Morning </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>Hi Simon, I would go for the date-comparison functions in the umbraco.library, e.g. DateGreaterThanToday
http://our.umbraco.org/wiki/reference/umbracolibrary
Tommy
Hi Tommy,
You could use DateGreaterThan or DateGreaterThanOrEqual (I wouldn't use DateGreaterThanToday, as it only does a day based comparison, not time which simon is after), but I didn't go that route as it sounded like he wanted a pure XSLT suggestion, but yea, definatley one to concider if you are wanting to do the same.
Matt
Hi Matt, sure - good point, if you need the time as well you have to use one of the other functions. But I would at any time prefer one of the standard extension methods over trying to parse the number yourself, relying on specific date/time format etc.
>Tommy
I think in this case where the time is not an input it is probably ok but if the time comparison was a value being passed in then I agree it's not really a safe option.
I think we more or less all agree ;-)
This also seems to work
Dan
Oooooh, thats a nice one.
Thanks, Simon, it helped me a lot!
Great, please make sure you vote for any posts you find helpful.
Simon
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.