Xslt Paging and Sorting according to attribute

08/12/2012 03:43

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

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

<xsl:stylesheet version="1.0"

xmlns:xsl="http://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:call-template name="DisplayPagingStatus"/>

                <xsl:for-each select="dsQueryResponse/Rows/Row">

                    <xsl:sort select="@Title"/>

                    <xsl:call-template name="DisplayRows"/>

                </xsl:for-each>

        </body>

    </html>

    </xsl:template>

 

    <xsl:template name="DisplayRows" match="Row">

        <p>

        <xsl:choose>

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

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

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

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

            </xsl:when>

            <xsl:otherwise>

            </xsl:otherwise>

        </xsl:choose>

        </p>

    </xsl:template>

 

    <xsl:template name="DisplayPagingStatus" 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>

        </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: C_Blog5 Position: 3 Item is on the page: 2

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