Check if 'later than' or 'earlier than' in XSLT
This is an open discussion with 6 replies, filed under XSLT.
Search
You can only use <
and >
on actual numbers
This should work:
<xsl:variable name="currenttime"><xsl:value-of select="/data/params/current-time"/></xsl:variable> <xsl:variable name="currenttimestripped"><xsl:value-of select="substring-before($currenttime, ':')"/></xsl:variable> <xsl:choose> <xsl:when test="$currenttimestripped > '10' and $currenttimestripped < '16'"> <p>We're open today untill <strong>16:00</strong></p> </xsl:when> <xsl:otherwise> <p>We'll be open tomorrow at <strong>10:00</strong></p> </xsl:otherwise> </xsl:choose>
or better:
<xsl:variable name="currenttimestripped" select="substring-before(/data/params/current-time, ':')"/> <xsl:choose> <xsl:when test="$currenttimestripped > '10' and $currenttimestripped < '16'"> <p>We're open today untill <strong>16:00</strong></p> </xsl:when> <xsl:otherwise> <p>We'll be open tomorrow at <strong>10:00</strong></p> </xsl:otherwise> </xsl:choose>
You can only use < and > on actual numbers
Sorry I should have been clear, I didn't expect that to work, it was just a demonstration of logic :)
Thanks for the example - much appreciated. I was actually hoping to avoid manipulating the string, but your second example is rather elegant, considering - so I'll be happy to use that!
$currenttimestripped > '10'
Would that logic not fail on any 10:00 - 10:59 ? because you are substringing only the first numbers of the time before the : and greater then 10 is 11
You should probably do
$currenttimestripped > '9'
What about using a four digit representation of time? Then you could even use minutes in those conditions!
<xsl:variable name="current-time-number" select="translate(/data/params/current-time, ':', '')"/> <xsl:choose> <xsl:when test="$current-time-number >= 1000 and $current-time-number < 1600"> <p>We're open today until <strong>16:00</strong></p> </xsl:when> <xsl:otherwise> <p>We'll be open tomorrow at <strong>10:00</strong></p> </xsl:otherwise> </xsl:choose>
(Note that I used >=
for the start time.)
Would that logic not fail on any 10:00 - 10:59 ? because you are substringing only the first numbers of the time before the : and greater then 10 is 11
You are right!
@michael-e: that's even better, there are some really handy xslt functions out there!
Create an account or sign in to comment.
I might be expecting too much from XSLT here, but I'm essentially trying to achieve the following logic:
I know there's some clever filter stuff in the backend, but does XSLT provide any methods for dealing with 'later than', 'earlier than' etc? Or will I have to push this out to JS/PHP for the logic?