Pagination in xslt

tia_kamakshi
tia_kamakshi used Ask the Experts™
on
Hi,

I have below xml and xslt working good. I need to add pagination in the result of my xslt transformation.

Can you please help me with pagination logic in my below xslt?

Thanks & Regards

<xsl:choose>
	<xsl:when test="$type = 'open' ">

		<xsl:for-each select="//SUMMARY[CATEGORY/@CATEGORYID = $categoryID or $categoryID = 0][SUBCATEGORYID = $subCategoryID or $subCategoryID = 0][//SUMMARY/FORMATTEDDATE >= $today]">
			<xsl:sort select="//SUMMARY/FORMATTEDDATE " />
			SOME STUFF
		</xsl:for-each>		

	</xsl:when>

</xsl:choose>

<root>

	<SUMMARYNODE>
		<SUMMARY>			
			<CATEGORY CATEGORYID="2">OPERATIONS PROCUREMENT</CATEGORY>
			<SUBCATEGORYID>4</SUBCATEGORYID>			
		</SUMMARY>
		
	</SUMMARYNODE>
	
	
	<SUMMARYNODE>
		<SUMMARY>			
			<CATEGORY CATEGORYID="2">XYZ</CATEGORY>
			<SUBCATEGORYID>6</SUBCATEGORYID>			
		</SUMMARY>
				
	</SUMMARYNODE>
	
	<SUMMARYNODE>
		<SUMMARY>			
			<CATEGORY CATEGORYID="5">ABC</CATEGORY>
			<SUBCATEGORYID>8</SUBCATEGORYID>			
		</SUMMARY>
			
	</SUMMARYNODE>

</root>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you can try to insert some page generating instructions every 5 items, using the following sample:

<xsl:if test="position() mod 5 = 0">
--- new page ---
</xsl:if>

Author

Commented:
Thanks for your reply.

I am not that strong in xslt. Can you please help me in fixing my xslt  that how previous, next pagination will work

Thanks again
<xsl:choose>
	<xsl:when test="$type = 'open' ">

		<xsl:for-each select="//SUMMARY[CATEGORY/@CATEGORYID = $categoryID or $categoryID = 0][SUBCATEGORYID = $subCategoryID or $subCategoryID = 0][//SUMMARY/FORMATTEDDATE >= $today]">
			<xsl:sort select="//SUMMARY/FORMATTEDDATE " />
			SOME STUFF
		</xsl:for-each>		

	</xsl:when>

</xsl:choose>

Open in new window

I can only give you general ideas.
Let's say we have an XML as follows:
<root>
<item>1</item>
<item>2</item>
.
.
</root>

The XSLT template could accept parameters, let's call them the "page" - for the page number (starting from 0) and the "num" - the number items on a page. A template like on the sample below will accept that params and output only the items for the specific page.
You need the "previous" and "next" commands (buttons?) to calculate the page number and pass the value to the template.



<xsl:template match="/root">
	<xsl:param name="page" select="0"/>
	<xsl:param name="num" select="6"/>
		
		<xsl:for-each select="item[position() &gt; $page * $num and position() &lt;= $page * $num + $num]">
			<xsl:value-of select="."/>,
  		</xsl:for-each>

</xsl:template>

Open in new window

Author

Commented:
Thanks for your reply.

Here is the xml and xslt as you advised. Please help me in fixing xslt code snippet

For this test, say I need 2 records per page.

Please advice

Thanks again
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<!--end of xsl declarations-->
	<!-- global variables -->
	<!--<xsl:param name="tenderUniqueId" />-->

	<xsl:template match="/">
		<xsl:param name="page" select="0"/>
		<xsl:param name="num" select="2"/>

		<xsl:for-each select="//SUMMARY[position() &gt; $page * $num and position() &lt;= $page * $num + $num]">
			<xsl:value-of select="CATEGORY"/>
			<BR></BR>
		</xsl:for-each>

	</xsl:template>

</xsl:stylesheet>


<root>

	<SUMMARYNODE>
		<SUMMARY>
			<CATEGORY CATEGORYID="2">OPERATIONS PROCUREMENT</CATEGORY>
			<SUBCATEGORYID>4</SUBCATEGORYID>
		</SUMMARY>

	</SUMMARYNODE>


	<SUMMARYNODE>
		<SUMMARY>
			<CATEGORY CATEGORYID="2">XYZ</CATEGORY>
			<SUBCATEGORYID>6</SUBCATEGORYID>
		</SUMMARY>

	</SUMMARYNODE>

	<SUMMARYNODE>
		<SUMMARY>
			<CATEGORY CATEGORYID="5">ABC</CATEGORY>
			<SUBCATEGORYID>8</SUBCATEGORYID>
		</SUMMARY>

	</SUMMARYNODE>

	<SUMMARYNODE>
		<SUMMARY>
			<CATEGORY CATEGORYID="5">DHGFHGF</CATEGORY>
			<SUBCATEGORYID>8</SUBCATEGORYID>
		</SUMMARY>

	</SUMMARYNODE>

	<SUMMARYNODE>
		<SUMMARY>
			<CATEGORY CATEGORYID="5">JGHJLUJL H</CATEGORY>
			<SUBCATEGORYID>8</SUBCATEGORYID>
		</SUMMARY>

	</SUMMARYNODE>

</root>

Open in new window

I changed the code a little to be compatible with the XML you provided.

To make the pagination work you need find a way to pass the current page number to the XSLT. It can be either stored in the XML or passed to the XSLT processor.


<xsl:param name="page" select="0"/>
<xsl:param name="num" select="2"/>

<xsl:template match="/">
	<xsl:for-each select="//SUMMARY">
		<xsl:if test="position() &gt; $page * $num and position() &lt;= $page * $num + $num">
			<xsl:value-of select="CATEGORY"/>
			<BR></BR>
  		</xsl:if>
	</xsl:for-each>
</xsl:template>

Open in new window

Author

Commented:
Many Thanks for providing xslt which works good when we give page no.

Now here how do we count no of pages and run loop till to print no of pages.

This will help me in doing pagination completly

Thanks again
To calculate the number of the pages, you can use the following expression:
select="ceiling( count( //SUMMARY ) div $num)"

Please explain more specific, what you mean by "pagination" and how you think it should work.

Author

Commented:
Sorry, I was bit away from my work. I am back again. Please see attached the sample image that how I require the pagination
Please help

 Pagination Image
I guess, this is a web application?
Let's say it's implemented on the server accessible as http://host.com
Then, each page number on your image should send the page number, like
http://host.com?p=1
http://host.com?p=2
http://host.com?p=3
etc.
On the server you need some code (what is it developing on? ASP? PHP?) to take that page number parameter "p" from the web request and pass as the parameter "page" as was shown in the previous samples.

The code below illustrates how could the pagination links be implemented on XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="page" select="1"/>
<xsl:param name="num" select="3"/>

	<xsl:template match="/root">
		<xsl:variable name="tot" select="count(SUMMARYNODE/SUMMARY)"/>
		<xsl:variable name="end" select="(1 + $page) * $num"/>
		<xsl:variable name="last-page" select="$tot div $num"/>
		
		<div style="width: 400px; font-weight: bold; font-size: xx-small; background-color: #DDC; padding: 0.5em;">
		<div style="float: left; color: 888;"><xsl:value-of select="$page * $num + 1"/>-<xsl:choose>
		   <xsl:when test="$end &gt; $tot"><xsl:value-of select="$tot"/></xsl:when>
		   <xsl:otherwise><xsl:value-of select="$end"/></xsl:otherwise>
		</xsl:choose> of <xsl:value-of select="$tot"/></div>

		<div style="float: right;">
		<a href="?p={$page - 1}"  style="text-decoration: none; color: #444;">
		<xsl:if test="$page &lt;= 0">
			<xsl:attribute name="disabled">true</xsl:attribute>
		</xsl:if>&lt;<xsl:text>&#xA0;</xsl:text></a>
		
		<xsl:for-each select="SUMMARYNODE/SUMMARY">
			<xsl:if test="position() mod $num = 1 and position() div $num &lt;= 10 + $page">
				<xsl:variable name="p" select="(position()-1) div $num"/>
				<xsl:choose>
			       <xsl:when test="$page = $p">
			       		<span style="color: #a43">
			       			<xsl:value-of select="$p + 1"/>
			       		</span></xsl:when>
			       <xsl:otherwise>
						<a href="?p={$p}"  style="text-decoration: none; color: #444;">
							<xsl:value-of select="$p + 1"/></a>
			       </xsl:otherwise>
			    </xsl:choose>
			    <xsl:text> </xsl:text>
			</xsl:if>
  		</xsl:for-each>
  		
		<a href="?p={$page + 1}" style="text-decoration: none; color: #444;">
		<xsl:if test="$page * $num &gt;= $tot - $num">
			<xsl:attribute name="disabled">true</xsl:attribute>
		</xsl:if>&gt;<xsl:text>&#xA0;</xsl:text></a>
		</div>
		<br style="clear: both;"/>
		</div>
</xsl:template>
		
</xsl:stylesheet>

Open in new window

Author

Commented:
Many Thanks for your help. I will come back to you on this

Kind Regards

Author

Commented:
Many Many Thanks for your great help
you're welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial