Posted By

iroybot on 11/08/11


Tagged

pages pagination Pager


Versions (?)

XSLT Pagination


 / Published in: XSLT
 

This Pagination XSLT is working just great for me. Been using it in numerous projects.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:html="http://www.w3.org/1999/xhtml">
  4. <xsl:output method="xml" indent="no" encoding="UTF-8"
  5. omit-xml-declaration="yes" />
  6.  
  7. <xsl:param name="currentPage" />
  8. <xsl:template match="/">
  9. <xsl:variable name="recordsPerPage" select="5" />
  10. <xsl:variable name="pageNumber">
  11. <xsl:choose>
  12. <!-- first page -->
  13. <xsl:when test="$currentPage &lt;= 0 or $currentPage = '' or $currentPage = 'NaN'">0</xsl:when>
  14. <!-- what was passed in -->
  15. <xsl:otherwise>
  16. <xsl:value-of select="$currentPage" />
  17. </xsl:otherwise>
  18. </xsl:choose>
  19. </xsl:variable>
  20. <xsl:variable name="numberOfRecords" select="count(/group/item)" />
  21.  
  22. <!-- The fun starts here -->
  23.  
  24. <xsl:call-template name="pagination">
  25. <xsl:with-param name="pageNumber" select="$pageNumber" />
  26. <xsl:with-param name="recordsPerPage" select="$recordsPerPage" />
  27. <xsl:with-param name="numberOfRecords" select="$numberOfRecords" />
  28. </xsl:call-template>
  29.  
  30. <ul class="listing self-clear">
  31. <xsl:for-each select="//item">
  32. <xsl:if
  33. test="position() &gt; $recordsPerPage * number($pageNumber) and position() &lt;= number($recordsPerPage * number($pageNumber) + $recordsPerPage )">
  34. <li>
  35. <xsl:attribute name="class">
  36. <xsl:if test="data[@alias='image'] = ''">
  37. no-img
  38. </xsl:if>
  39. <xsl:if test="position() = $recordsPerPage * (number($pageNumber) + 1)">last</xsl:if></xsl:attribute>
  40. <h3>
  41. <a href="?p={position()}">
  42. <xsl:value-of select="text()" />
  43. </a>
  44. </h3>
  45. </li>
  46. </xsl:if>
  47. </xsl:for-each>
  48. </ul>
  49.  
  50. <xsl:call-template name="pagination">
  51. <xsl:with-param name="pageNumber" select="$pageNumber" />
  52. <xsl:with-param name="recordsPerPage" select="$recordsPerPage" />
  53. <xsl:with-param name="numberOfRecords" select="$numberOfRecords" />
  54. </xsl:call-template>
  55.  
  56. </xsl:template>
  57.  
  58. <xsl:template name="pagination">
  59. <xsl:param name="pageNumber" />
  60. <xsl:param name="recordsPerPage" />
  61. <xsl:param name="numberOfRecords" />
  62.  
  63. <div class="pagination">
  64. <div class="wrapper">
  65.  
  66. <xsl:if
  67. test="(($pageNumber +1 ) * $recordsPerPage) &lt; ($numberOfRecords)">
  68. <a href="?page={$pageNumber +1}" class="next">Next</a>
  69. </xsl:if>
  70.  
  71. <xsl:if test="$pageNumber &gt; 0">
  72. <a href="?page={$pageNumber -1}" class="prev">Prev</a>
  73. </xsl:if>
  74.  
  75. <span class="page-nos">
  76. Page
  77. <xsl:call-template name="for.loop">
  78. <xsl:with-param name="i">
  79. 1
  80. </xsl:with-param>
  81. <xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
  82. <xsl:with-param name="count"
  83. select="ceiling(count(//item)div $recordsPerPage)"></xsl:with-param>
  84. </xsl:call-template>
  85. </span>
  86.  
  87. </div>
  88. </div>
  89. </xsl:template>
  90.  
  91. <xsl:template name="for.loop">
  92. <xsl:param name="i" />
  93. <xsl:param name="count" />
  94. <xsl:param name="page" />
  95. <xsl:if test="$i &lt;= $count">
  96. <span>
  97. <xsl:if test="$page != $i">
  98. <a href="?page={$currentPage}">
  99. <xsl:value-of select="$i" />
  100. </a>
  101. </xsl:if>
  102. <xsl:if test="$page = $i">
  103. <xsl:value-of select="$i" />
  104. </xsl:if>
  105. </span>
  106. </xsl:if>
  107.  
  108. <xsl:if test="$i &lt;= $count">
  109. <xsl:call-template name="for.loop">
  110. <xsl:with-param name="i">
  111. <xsl:value-of select="$i + 1" />
  112. </xsl:with-param>
  113. <xsl:with-param name="count">
  114. <xsl:value-of select="$count" />
  115. </xsl:with-param>
  116.  
  117. <xsl:with-param name="page">
  118. <xsl:value-of select="$page" />
  119. </xsl:with-param>
  120. </xsl:call-template>
  121. </xsl:if>
  122. </xsl:template>
  123. </xsl:stylesheet>

Report this snippet  

You need to login to post a comment.