使用XSLT实现内容管理系统中相邻文章导航功能

本文假设你已对umbraco的xslt已经有一定的了解。对于还不知道XSLT为何物的,请google参阅其他文章。

第一步,创建一个名为ArticleNav.xslt XSLT文件,

create xslt and macro

并贴入以下内容(注意同时勾选创建宏)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">

<xsl:variable name="Previous" select="$currentPage/preceding-sibling::node[1]/@id"/>
<xsl:variable name="Next" select="$currentPage/following-sibling::node/@id"/>

<div class="Left">«上一篇:
    <xsl:choose>
    <xsl:when test="count($currentPage/preceding-sibling::node) > 0">
            <a href="{umbraco.library:NiceUrl($Previous)}">
            <xsl:value-of select="$currentPage/preceding-sibling::node[1]/@nodeName"/>
        </a>
    </xsl:when>
    <xsl:otherwise>
    没有了
    </xsl:otherwise>
    </xsl:choose>
</div>

<div class="Right">下一篇:
    <xsl:choose>
    <xsl:when test="count($currentPage/following-sibling::node) > 0">
        <a href="{umbraco.library:NiceUrl($Next)}">
            <xsl:value-of select="$currentPage/following-sibling::node[1]/@nodeName"/> »
        </a>
    </xsl:when>
    <xsl:otherwise>
        没有了 »
    </xsl:otherwise>
    </xsl:choose>
</div>

</xsl:template>

</xsl:stylesheet>

 

第二步,在文章显示模板相应位置增加以下内容

<div class="bottomarticleNav"><umbraco:Macro Alias="XSLTArticleNav" runat="server"></umbraco:Macro></div>

Insert macro to template

OK,现在浏览文章页面刷新下试试,看看效果是不是出来了。

最终效果图

当然,稍加改造,也可以把它整成文字链接,并显示标题。