Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3451 karma points c-trib
    Jul 06, 2010 @ 13:53
    Simon Dingley
    0

    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 &gt; Exslt.ExsltDatesAndTimes:time('12:00:00') and $currentTime &lt; Exslt.ExsltDatesAndTimes:time('17:00:00')">
        Good Afternoon
    </xsl:when>
    <xsl:when test="$currentTime &gt; Exslt.ExsltDatesAndTimes:time('17:00:00') and $currentTime &lt; 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.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 06, 2010 @ 13:58
    Matt Brailsford
    1

    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

  • Simon Dingley 1474 posts 3451 karma points c-trib
    Jul 06, 2010 @ 14:18
    Simon Dingley
    0

    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 "&#x00A0;"> ]>
    <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 &gt; $midday and $currentTime &lt; $evening">
        Good Afternoon
    </xsl:when>
    <xsl:when test="$currentTime &gt; $evening and $currentTime &lt; $midnight">
        Good evening
    </xsl:when>
    <xsl:otherwise>
        Good Morning
    </xsl:otherwise>
    </xsl:choose>
    
    </xsl:template>
    </xsl:stylesheet>
  • Tommy Poulsen 514 posts 708 karma points
    Jul 06, 2010 @ 14:18
    Tommy Poulsen
    0

    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

     

     

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 06, 2010 @ 15:03
    Matt Brailsford
    0

    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

  • Tommy Poulsen 514 posts 708 karma points
    Jul 06, 2010 @ 17:25
    Tommy Poulsen
    0

    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

  • Simon Dingley 1474 posts 3451 karma points c-trib
    Jul 06, 2010 @ 17:30
    Simon Dingley
    0

    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.

  • Tommy Poulsen 514 posts 708 karma points
    Jul 06, 2010 @ 18:32
    Tommy Poulsen
    0

    I think we more or less all agree ;-)

  • dandrayne 1138 posts 2262 karma points
    Jul 06, 2010 @ 20:35
    dandrayne
    1

    This also seems to work

    <xsl:variable name="hours" select="Exslt.ExsltDatesAndTimes:hourinday()"/>
    <xsl:choose>
    <xsl:when test="$hours &gt; 00 and $hours &lt; 04">
    <xsl:text>Time for bed</xsl:text>
    </xsl:when>
    <xsl:otherwise>
    <xsl:text>Not bedtime yet</xsl:text>
    </xsl:otherwise>
    </xsl:choose>

    Dan

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 06, 2010 @ 20:41
    Matt Brailsford
    0

    Oooooh, thats a nice one.

  • Ilya 1 post 21 karma points
    Apr 12, 2011 @ 13:48
    Ilya
    0

    Thanks, Simon, it helped me a lot!

  • Simon Dingley 1474 posts 3451 karma points c-trib
    Apr 12, 2011 @ 13:56
    Simon Dingley
    0

    Great, please make sure you vote for any posts you find helpful.

    Simon

  • 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.

    Continue discussion

Please Sign in or register to post replies