XSL: for-each with max items

by Pieter Brinkman 5. August 2009 09:21

While working with Umbraco and Sitecore I learned some Xsl tricks.

The following example shows how to show the first 10 items in a HTML list.

[code:xml]
<ul>
  <xsl:for-each select="*">
    <xsl:if test="position()&lt;='9'">
      <li>
 <a href="{@link}">
   <xsl:value-of select="@name"/>
  </a>
      </li>
    </xsl:if>
  </xsl:for-each>
</ul>
[/code]

 

Categories: XSL

Comments

Vince
Vince Netherlands on 8/6/2009 2:41:17 PM

In this case (when your just limiting results) it's probably better to put the position into the for each statement. Something like:

      <xsl:for-each select="position()&lt;='9'">

using it in the <if> statement is great for displaying a couple of items differently (for example the first 3)

Vince
Vince United States on 8/10/2009 4:26:08 AM

correction:

<xsl:for-each select="item[position()&lt;='2']">