Search

I am working on a calendar and, new to xslt, bumped up a problem which I have not been able to solve on my own.

I will have two and perhaps more different types of event which are set in a selectbox called ‘choose-which-calendar’ in the events section which the calendar will display.

There is a url parameter on the calendar page called event-type. The datasource will pull all the events into the xml and the stylesheet is supposed to process the xml using the url parameter ‘event-type’. Eventually, if there is no event-type ($event-type=”) I want all of the events to display, at the moment I can’t get the xslt to evaluate the parameter. The event-type exists as a parameter and I can set it using the url.

Here is the bare-bones template

<xsl:template match="events/entry">
<xsl:choose>
 <xsl:when test="choose-which-calendar/item[@handle='$event-type']" >
   <div class="eventContent">
   <h2 class="summary"><xsl:value-of select="event" /></h2>
   </div><!--End of eventContent-->
  </xsl:when>
  <xsl:otherwise>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

Eventually I would like the test= to include an empty event-type parameter

 <xsl:when test="choose-which-calendar/item[@handle='$event-type']|$event-type=''" >

TIA

Fred, try doing this:

<xsl:when test="not($event-type)">
    <!-- do default stuff -->
</xsl:when>
<xsl:otherwise>
    <!-- do `choose-which-calendar/item[@handle='$event-type']` stuff here intstead -->
</xsl:otherwise>

Thanks wtdtan,

What I want to do is go through the event elements and if the event-type url parameter matches the choose-which-calendar item in the event record, process the record.

Eg: If the url is ‘my-site/calendar/teaching’, records where the ‘choose-which-calendar’ is ‘teaching’ will display. If the url is ‘my-site/calendar/exhibitions’ records where the ‘choose-which-calendar’ is ‘exhibitions’ will display.

If the url is ‘my-site/calendar’, all the records will display.

I can’t seem to make a connection in the xpath between the handle of item and the url parameter. If I hard-wire the evaluation,

<xsl:when test="choose-which-calendar/item[@handle='teaching']" >

it processes the correct records.

If I set the url parameter http://my-site/calendar/teaching and use

<xsl:when test="choose-which-calendar/item[@handle='$event-type']" >

it doesn’t.

There are a few things at play here:

If $event-type is not set, and it is a URL Parameter assigned to your page, it will still exist but will have a blank value. Therefore you can check it using:

<xsl:if test="$event-type=''">
    ...
</xsl:if>

Secondly, your attribute match using the value of $event-type may not be working as intended since you’re surrounding it with apostrophe’s in your test. This will mean the XPath is using it as a literal value (looking for an item where the value actually is $event-type) rather than evaluating its value. This can be fixed by removing the quotes:

<xsl:when test="choose-which-calendar/item[@handle=$event-type]">
    ...
</xsl:when>

Recently I’ve started approaching situations like this by using more than one template. I use the core template in my page to act as a controller, deciding which templates to fire — then these apply smaller templates to write the HTML. In your example, it would be achieved like this:

<xsl:template match="data">
    <xsl:choose>
        <xsl:when test="$event-type=''">
            <xsl:apply-templates select="events/entry"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates select="events/entry[choose-which-calendar/item/@handle=$event-type]"/>
        </xsl:otherwise>
    </xsl:choose>       
</xsl:template>

<xsl:template match="events/entry">
    <div class="eventContent">
        <h2>
            <xsl:value-of select="event"/>
        </h2>
    </div>
</xsl:template>

When $event-type is empty you match on all event entry nodes. Otherwise you want to select all entries where the event types match.

Don’t forget that this would ideally be achieved in your Data Source. You could filter the Choose Which Calendar field in the DS and add the following as a filter:

{$event-type}

This will do the filtering at the database level which is more efficient. If the $event-type parameter is not set in the URL, the Data Source will ignore the filter entirely, thereby returning all entries. But if it is set, it will filter them for you. You then would not need to worry about filtering by $event-type in the XSL.

Hope this helps!

Thanks Nick

I thought of filtering the datasource but discarded that route, because I hadn’t clocked that all records would display if the parameter isn’t set.

Using datasource filtering could also be useful, because I hadn’t allowed for erroneous url parameters that don’t exist in the data like ‘mysite/calendar/massagesessions’, which would presumably produce no records and that I could test for in the xslt and display an error message .

Do you have extra sort params on the pages as well?

I seem to be encountering an issue where I have a parameter in the url called $hours and this is looking for full-time or part-time as values from a selct box.

(5 months late, but it’s worth adding…)

I thought of filtering the datasource but discarded that route, because I hadn’t clocked that all records would display if the parameter isn’t set.

If you wanted the reverse, i.e. if $event-type is not present you want to still return no entries rather than all, you can achieve this in one of two ways:

a) use the “Required URL Parameter” box in the Data Source and add in the $event-type parameter. This means the DS will only try and return entries when this parameter has a value.

b) change your filter to something like {$event-type:0}. The colon acts as a fall-back, so if the parameter cannot be resolved it will use a literal value of 0. Providing you have no entries that have a value of 0, you’ll get no entries back.

ok cool I get the thinking behind that.. nice and clean too!

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