For+Loops+XSLT

I'm new at XSLT but one obvious thing I wanted to do was write a for loop with a counter. I read that I should do this with recusion and templates and to treat it as a function language. I didn't have the time, so I just did this:

  ...  

  <!-- max iterations -->  

  <xsl:variable name="count">5</xsl:variable>  

  <!-- begin for loop -->  

  <xsl:for-each select="list_of_items/item">  

    <!-- loop test -->  

    <xsl:if test="position() <= $count">  

      <!-- body of the loop -->  

      <li>  

        <xsl:value-of select="item_description"/>  

      </li>  

    </xsl:if>  

  </xsl:for-each>  

  ...

Basically, I set the count variable to 5 and then use an if statment to see if anything in the body of the loop should be executed. It's a terrible hack, but doing it the right way seemed like overkill for what I was doing.

Note: in the test statement, you'll actually have to type < instead of < to make it work

Posted: November 19, 2007 | | comments

spinner