Simple paging in results

08/12/2012 03:05

<?xml version="1.0" encoding="iso-8859-1"?>

<!-- Edited by Lukas Sevcik® -->

<xsl:stylesheet version="1.0"

xmlns:xsl="https://www.w3.org/1999/XSL/Transform">

    <xsl:param name="pageNr" select="2"/>

    <xsl:param name="pageSize" select="2"/>

    <xsl:variable name="startPosition" select="$pageNr*$pageSize - $pageSize+1"/>

    <xsl:variable name="endPosition" select="$pageSize*$pageNr"/>

 

    <xsl:template match="/">

    <html>

        <body>

            <h2>Paging</h2>

            <xsl:apply-templates/>

        </body>

    </html>

    </xsl:template>

 

    <xsl:template match="Row">

    <p>

        Title: <xsl:value-of select="@Title"/>

        Position: <xsl:value-of select="position()"/>

        <xsl:choose>

            <xsl:when test="(not(position() &gt; $endPosition)) and (position() &gt;= $startPosition)">

                Item is on the page: <xsl:value-of select="$pageNr"/>

            </xsl:when>

            <xsl:otherwise>

                Item is out of page

            </xsl:otherwise>

        </xsl:choose>

    </p>

    </xsl:template>

 

    <xsl:template match="Rows">

    <p>

        Page Nr: <xsl:value-of select="$pageNr"/>

        Page size: <xsl:value-of select="$pageSize"/>

        Start position: <xsl:value-of select="$startPosition"/>

        End position: <xsl:value-of select="$endPosition"/>

        <br></br>

        <xsl:text>----------------------------------</xsl:text>

    <xsl:apply-templates/>

    </p>

    </xsl:template>

</xsl:stylesheet>

 

Input Xml:

<?xml version="1.0" encoding="utf-8"?>

<dsQueryResponse>

    <Rows>

        <Row Title="E_Blog1"/>

        <Row Title="A_Blog2"/>

        <Row Title="D_Blog3"/>

        <Row Title="B_Blog4"/>

        <Row Title="C_Blog5"/>

        <Row Title="F_Blog6"/>

    </Rows>

</dsQueryResponse>

 

Result:

Paging

Page Nr: 2 Page size: 2 Start position: 3 End position: 4
----------------------------------

Title: E_Blog1 Position: 1 Item is out of page

Title: A_Blog2 Position: 2 Item is out of page

Title: D_Blog3 Position: 3 Item is on the page: 2

Title: B_Blog4 Position: 4 Item is on the page: 2

Title: C_Blog5 Position: 5 Item is out of page

Title: F_Blog6 Position: 6 Item is out of page