title: selecting top X items from Xml using XSLT in Umbraco
body: <p>I have an Umbraco based blog and I want to display the top 10 posts in each category on the front page of my blog.  Category is just another property on my Document Type.  </p>
  
  <p>Umbraco basically stores its contents as XML, so category is an element of my blog post xml.</p>
  
  <p>I am using Blog4Umbraco as the base blog package on Umbraco and it stores blog posts ("documents") in a date based hierarchy of year/month/day.  e.g. if I created a blog post "Help me with this" today, it would be under /2011/02/16/help-me-with-this.</p>
  
  <p>I have xlst to display all posts. It iterates through each folder and then sorts on date.  The problem is I only want the 10 most recent blog posts for each category.  The following xslt displays selects all blog posts for a particular category.  How do I limit it to only 10, given that I may have to span multiple folders (multiple days, months, or even years) to get 10?</p>
  
  <pre><code>&lt;msxsl:script implements-prefix='ss' language='CSharp'&gt;  
  &lt;![CDATA[
  public string monthName(int monthNum)
  {
  DateTime date = new DateTime(1,monthNum,1);
  return date.ToString("MMMM");
  }
  public int dayNumber(string date)
  {
  DateTime mydate = DateTime.Parse(date);
  return mydate.Day;
  }]]&gt;
  &lt;/msxsl:script&gt;
  
  &lt;xsl:output method="html" omit-xml-declaration="yes"/&gt;
  
  &lt;xsl:param name="currentPage"/&gt;
  &lt;xsl:param name="topic" select="/macro/category"/&gt;
  
  &lt;xsl:template match="/"&gt;
  &lt;div id="archive"&gt;
    &lt;h3&gt;&lt;xsl:value-of select="$topic" /&gt;&lt;/h3&gt;
    &lt;xsl:for-each select="$currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']/node [@nodeTypeAlias = 'DateFolder']"&gt;
    &lt;xsl:sort select="number(@nodeName)" data-type="number" order="descending"/&gt;
    &lt;xsl:for-each select="./node [@nodeTypeAlias = 'DateFolder']"&gt;
    &lt;xsl:sort select="number(@nodeName)" data-type="number" order="descending"/&gt;
  
    &lt;xsl:for-each select=".//node [@nodeTypeAlias = 'BlogPost']"&gt;
      &lt;xsl:sort select="ss:dayNumber(@createDate)" data-type="number" order="descending"/&gt;
    &lt;xsl:if test="./data[@alias='topic'] = $topic"&gt;
    &lt;div class="listing"&gt;
      &lt;a href="{umbraco.library:NiceUrl(@id)}"&gt;&lt;xsl:value-of select="@nodeName"/&gt;&lt;/a&gt; &lt;br/&gt; 
      &lt;small&gt;
        Posted: &lt;xsl:value-of select="umbraco.library:LongDate(@createDate)"/&gt;
        By: &lt;xsl:value-of select="@writerName"/&gt;&lt;br /&gt;
      &lt;/small&gt;
      &lt;xsl:if test="string-length(./data [@alias = 'blogSummary']) = 0"&gt;
        &lt;xsl:value-of select="substring(umbraco.library:StripHtml(./data [@alias = 'bodyText']), 1, 200)"/&gt;...
      &lt;/xsl:if&gt;
  
      &lt;xsl:if test="string-length(./data [@alias = 'blogSummary']) &amp;gt; 0"&gt;
        &lt;xsl:value-of select="./data [@alias = 'blogSummary']"/&gt;
      &lt;/xsl:if&gt;
  
  
    &lt;/div&gt;
    &lt;/xsl:if&gt;
  &lt;/xsl:for-each&gt;
  &lt;/xsl:for-each&gt;
  &lt;/xsl:for-each&gt;
  &lt;/div&gt;
  &lt;/xsl:template&gt;
  </code></pre>