Click here to Skip to main content
Licence CPOL
First Posted 10 Aug 2005
Views 40,842
Bookmarked 12 times

Pagination using XSL

By VivekAyer | 16 Apr 2010 | Unedited contribution
To display data in multiple pages using XSL.
10 votes, 58.8%
1

2

3
1 vote, 5.9%
4
6 votes, 35.3%
5
1.69/5 - 17 votes
μ 1.69, σa 3.45 [?]

Introduction

Below is a way to display your xml in multiple pages with Prev and Next links. You could customize it to display any number of entries on a page. It takes in 2 parameters that could be passed as querystrings to the page.

Page - should hold the value of which page is currently displayed.
PageSize - should hold the value of how many entries should be displayed in a page.
_dirresult - replace this with the url to the page that does the transformation

Note: First page id is 0

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>">
<xsl:output method="html" version="3.2" encoding="ISO-8859-1"/>
<xsl:param name="Page" select="0" />
<xsl:param name="PageSize" select="1" />
 
<xsl:template name="results" match="/">


<xsl:variable name="mycount" select="count(root/customer)"/>
<xsl:variable name="selectedRowCount" select="floor((number($numberOfRecords)-1) div $recordsPerPage)+1"/>


      <xsl:for-each select="root/customer">
       <!-- Pagination logic -->
       <xsl:if test="position() &gt;= ($Page * $PageSize) + 1">
        <xsl:if test="position() &lt;= $PageSize + ($PageSize * $Page)">


         <!-- Do display here -->
         
        </xsl:if>
       </xsl:if>
      </xsl:for-each>


      <!-- Prev link for pagination -->
      <xsl:choose>
       <xsl:when test="number($Page)-1 &gt;= 0">&#160;
        <A>
         <xsl:attribute name="href">_dirresult?page=<xsl:value-of select="number($Page)-1"/>&amp;pagesize=<xsl:value-of 
select="$PageSize"/></xsl:attribute>
          &lt;&lt;Prev
        </A>
       </xsl:when>
       <xsl:otherwise>
        <!-- display something else -->
       </xsl:otherwise>
      </xsl:choose>
      
      <xsl:if test="$selectedRowCount &gt; 1">
       &#160;<b class="blacktext"><xsl:value-of select="number($Page)+1"/>&#160;of&#160;<xsl:value-of 
select="number($selectedRowCount)"/></b>&#160;
      </xsl:if>
               
      <!-- Next link for pagination -->
      <xsl:choose>
       <xsl:when test="number($Page)+1 &lt; number($selectedRowCount)">&#160;
        <A>
         <xsl:attribute name="href">_dirresult?page=<xsl:value-of select="number($Page)+1"/>&amp;pagesize=<xsl:value-of 
select="$PageSize"/></xsl:attribute>
          Next&gt;&gt;
        </A>
       </xsl:when>
       <xsl:otherwise>
        <!-- display something else -->
       </xsl:otherwise>
      </xsl:choose>

   
 </xsl:template>
</xsl:stylesheet>

<pre>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

VivekAyer



United Kingdom United Kingdom

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerRE: Pagination using XSL Pinmemberq2581:16 21 Nov '08  
Probably a bit more efficient to just select what we want rather than select all then filter... Perhaps.... Thus a predicate in the for-each rather then consecutive nested if's...
 

<xsl:for-each
         select="root/customer[position() &gt;= (($Page * $PageSize) + 1)
         AND position() &lt;= ($PageSize + ($PageSize * $Page))">
 
<!-- Do display here -->
 
</xsl:for-each>
QuestionNot understood properly Pinmemberrascalpranesh23:53 23 Jun '08  
QuestionCannot go next page, the parameter Page and PageSize not changing on next page PinmemberPaillaRajashekarReddy2:18 13 May '08  
AnswerRe: Cannot go next page, the parameter Page and PageSize not changing on next page PinmembersalahudeenDiamond6:14 10 Mar '09  
GeneralCannot go next page, the parameter Page and PageSize not changing on next page PinmemberEhteshamJax2:15 15 Aug '07  
Questionhow to use pagination to list the next records using next link PinPopularmemberchennaivijayan23:11 31 May '06  
AnswerRe: how to use pagination to list the next records using next link PinmemberEhteshamJax2:17 15 Aug '07  
GeneralRe: how to use pagination to list the next records using next link PinmembersalahudeenDiamond6:15 10 Mar '09  
GeneralRe: how to use pagination to list the next records using next link Pinmemberhighpowered17:42 22 Oct '10  
QuestionQuick questions. Pinmembereddiephoenix5:33 2 Feb '06  
GeneralDuh! PinmemberDon Miguel0:40 11 Aug '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 16 Apr 2010
Article Copyright 2005 by VivekAyer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid