Avatar of tia_kamakshi
tia_kamakshi
Flag for United Arab Emirates asked on

Pagination in xslt

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

Web Languages and StandardsXML

Avatar of undefined
Last Comment
zc2

8/22/2022 - Mon
zc2

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>
tia_kamakshi

ASKER
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

zc2

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
tia_kamakshi

ASKER
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

zc2

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

tia_kamakshi

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
zc2

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.
tia_kamakshi

ASKER
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
ASKER CERTIFIED SOLUTION
zc2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
tia_kamakshi

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

Kind Regards
Your help has saved me hundreds of hours of internet surfing.
fblack61
tia_kamakshi

ASKER
Many Many Thanks for your great help
zc2

you're welcome