Search

I might be expecting too much from XSLT here, but I'm essentially trying to achieve the following logic:

    <xsl:choose>
        <xsl:when test="/data/params/current-time &gt; '10:00' and /data/params/current-time &lt; '16:00'">
            <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>

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?

You can only use &lt; and &gt; 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 &gt; '10' and $currenttimestripped &lt; '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 &gt; '10' and $currenttimestripped &lt; '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 &gt; '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 &gt; '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 &gt;= 1000 and $current-time-number &lt; 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 &gt;= 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.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details