在 Umbraco 中使用 XSLT 从 Xml 中选择前 X 个项目
我有一个基于 Umbraco 的博客,我想在博客的首页上显示每个类别中排名前 10 的帖子。类别只是我的文档类型的另一个属性。
Umbraco 基本上将其内容存储为 XML,因此类别是我的博客文章 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>
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.
Umbraco basically stores its contents as XML, so category is an element of my blog post xml.
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.
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?
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代替:
使用:
Instead of:
Use: