我有一个基于Umbraco的博客,我想在我博客的首页上显示每个类别的前10个帖子.类别只是我的文档类型的另一个属性. Umbraco基本上将其内容存储为XML,因此category是我的博客文章xml的一个元
Umbraco基本上将其内容存储为XML,因此category是我的博客文章xml的一个元素.
我使用Blog4Umbraco作为Umbraco的基础博客包,它将博客帖子(“文档”)存储在基于日期的年/月/日层次结构中.例如如果我今天创建了一个博客文章“帮助我”,它将在/ 2011/02/16 / help-me-with-this.
我有xlst来显示所有帖子.它遍历每个文件夹,然后按日期排序.问题是我只想要每个类别的10篇最新博客文章.以下xslt显示选择特定类别的所有博客帖子.我如何将其限制为10只,因为我可能需要跨越多个文件夹(多天,几个月甚至几年)才能获得10个?
<msxsl:script implements-prefix='ss' language='CSharp'> <![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; }]]> </msxsl:script> <xsl:output method="html" omit-xml-declaration="yes"/> <xsl:param name="currentPage"/> <xsl:param name="topic" select="/macro/category"/> <xsl:template match="/"> <div id="archive"> <h3><xsl:value-of select="$topic" /></h3> <xsl:for-each select="$currentPage/ancestor-or-self::node [@nodeTypeAlias = 'Blog']/node [@nodeTypeAlias = 'DateFolder']"> <xsl:sort select="number(@nodeName)" data-type="number" order="descending"/> <xsl:for-each select="./node [@nodeTypeAlias = 'DateFolder']"> <xsl:sort select="number(@nodeName)" data-type="number" order="descending"/> <xsl:for-each select=".//node [@nodeTypeAlias = 'BlogPost']"> <xsl:sort select="ss:dayNumber(@createDate)" data-type="number" order="descending"/> <xsl:if test="./data[@alias='topic'] = $topic"> <div class="listing"> <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a> <br/> <small> Posted: <xsl:value-of select="umbraco.library:LongDate(@createDate)"/> By: <xsl:value-of select="@writerName"/><br /> </small> <xsl:if test="string-length(./data [@alias = 'blogSummary']) = 0"> <xsl:value-of select="substring(umbraco.library:StripHtml(./data [@alias = 'bodyText']), 1, 200)"/>... </xsl:if> <xsl:if test="string-length(./data [@alias = 'blogSummary']) > 0"> <xsl:value-of select="./data [@alias = 'blogSummary']"/> </xsl:if> </div> </xsl:if> </xsl:for-each> </xsl:for-each> </xsl:for-each> </div> </xsl:template>代替:
<xsl:for-each select=".//node [@nodeTypeAlias = 'BlogPost']"> <xsl:sort select="ss:dayNumber(@createDate)" data-type="number" order="descending"/> <xsl:if test="./data[@alias='topic'] = $topic"> <div class="listing"> <!-- Some processing here --> </div> </xsl:if> </xsl:for-each>
使用:
<xsl:for-each select= ".//node [@nodeTypeAlias = 'BlogPost'] [data[@alias='topic'] = $topic] "> <xsl:sort select="ss:dayNumber(@createDate)" data-type="number" order="descending"/> <xsl:if test="not(position() > 10)"> <div class="listing"> <!-- Some processing here --> </div> </xsl:if> </xsl:for-each>