分页功能应该说是web开发中最基本的功能了,常规的做法是通过查询sql语句进行分页数据显示。但在umbraco中却不是这样子的。而且通过xpath中的postion来定位。如下代码
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp “ ”> ]> <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=”recordsPerPage” select=”10″/> <xsl:variable name=”pageNumber” > <xsl:choose> <!– first page –> <xsl:when test=”umbraco.library:RequestQueryString(’page’) <= 0 or string(umbraco.library:RequestQueryString(’page’)) = ” or string(umbraco.library:RequestQueryString(’page’)) = ‘NaN’”>0</xsl:when> <!– what was passed in –> <xsl:otherwise> <xsl:value-of select=”umbraco.library:RequestQueryString(’page’)”/></xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name=”numberOfRecords” select=”count($currentPage/node)”/> <!– The fun starts here –> <ul> <xsl:for-each select=”$currentPage/node [string(data [@alias=’umbracoNaviHide’]) != ‘1′]”> <xsl:if test=”position() > $recordsPerPage * number($pageNumber) and position() <= number($recordsPerPage * number($pageNumber) + $recordsPerPage )"> <li><a href=”{umbraco.library:NiceUrl(@id)}”> <xsl:value-of select=”@nodeName”/> </a> </li> </xsl:if> </xsl:for-each> </ul> <xsl:if test=”$pageNumber > 0″> <a href=”?page{$pageNumber -1}”>previous </a> </xsl:if> <xsl:if test=”(($pageNumber +1 ) * $recordsPerPage) < ($numberOfRecords)”> <a href=”?page={$pageNumber +1}”>next</a> </xsl:if> </xsl:template> </xsl:stylesheet>
简单说明
count($currentPage/node)就是计算根节点下以node开头的childNode的个数
position()指的是当前节点的位置,即从1开始的数值