Part of: Main

 

Page Actions: Edit PageRecent ChangesPage HistoryPrintable View

On the 2005 homepage I wanted to count out for events to display in one div. The total of the events listed was to come from two nodes, Today and Upcoming shown in the XML here.

 <calendar>
	<today>
		<event>
			<title>Teaching Opportunity/title>
		</event>
		<event>
			<title>Walker without Walls</title>
		</event>
	</today>
	<upcoming>
		<event>
			<title>Talking Dance</title>
		</event>
		<event>
			<title>Talking Dance Series</title>
		</event>
		<event>
			<title>Walker Dance</title>
		</event>
		<event>
			<title>Performance Company</title>
		</event>
		<event>
			<title>Talking Dance</title>
		</event>
	</upcoming>
 </calendar>

I broke the counting up into two templates to iterate through the two nodes and pass the counting and totals in as parameters. My XSL looked like this to call the first template

 <div class="smPoster green">
    <h2>Calendar</h2>
           <div id="title">
                TODAY:
           </div>
           <xsl:call-template name="now">
                <xsl:with-param name="counter" select="1" />
           </xsl:call-template>
 </div>

The first part of the template that iterates through the "Today" node works like this

 <xsl:template name="now">
      <xsl:param name="limit" select="3"/>
      <xsl:param name="counter" />
      <xsl:param name="eventNum" select="count(//today/event)"/>

That just defined the paramaters that this template will use. Limit will make sure we don't go over four events (i call the template one more time after the limit is reached that is why it is set to 3 not 4) counter lets us keep a total count instead of doing it with position. That way we can pass the counter parameter on later when we call our second template. eventNum tells us how many event nodes there are in the today node so we know when to switch from events in today and start pulling events from the upcoming node instead. after i defined the parameters the template went on like this.

 <xsl:if test="$counter <= $limit">
  <div id="calendarParag">
     <xsl:value-of select="//calendar/today/event[$counter]/title"/>
  </div>

That just checks to see if the number of events displayed is less than our limit then it gets the title of the event out and ...

   <xsl:if test="$counter < $eventNum">
      <xsl:call-template name="now">
          <xsl:with-param name="counter" select="$counter + 1" />
      </xsl:call-template>
   </xsl:if>

That checked to see if the counter is still less than the number of events. If it is l call this template (the one we are in right now) again after the $counter gets incremented. If we have seen all the events in the Today. After we have gone through the today node we can put down the different title dive and check the upcoming node like this

   <div>
      COMING SOON:
   </div>
   <xsl:call-template name="soon">
      <xsl:with-param name="counter" select="$counter"/>
      <xsl:with-param name="tCounter" select="1"/>
      <xsl:with-param name="limit" select="$limit"/>
   </xsl:call-template>

That passed along all the parameters we knew before but it also passes in one called tempCounter. If we don't have an independat counter and there are less than 4 events total in both nodes the XSL will go into an infinite loop and die. next we just close off our xsl:if 's and we get out of this template.

   </xsl:if>
 </xsl:if>
 </xsl:template>

The second template just has to check to see if the counter level has been reached. If it has not then it calls itself and gets the data for another event.

  <xsl:template name="soon">
    <xsl:param name="counter" />
    <xsl:param name="tCounter"/>
    <xsl:param name="limit" />
    <xsl:if test="$counter <= $limit">
        <div id="calendarParag">
            <xsl:value-of select="//upcoming/event[$tCounter]/title"/>
        </div>
        <xsl:call-template name="soon">
            <xsl:with-param name="counter" select="$counter + 1" />
            <xsl:with-param name="tCounter" select="$tCounter + 1" />
            <xsl:with-param name="limit" select="$limit"/>
        </xsl:call-template>
    </xsl:if>
 </xsl:template>
Page last modified on November 16, 2005, at 03:59 PM
Page Actions: Edit PageRecent ChangesPage HistoryPrintable View