Add Namespaces:
---------------------------------
using System.Xml.Xsl;
using System.IO;
using System.Xml.XPath;
using System.Xml;
using System.Text;
-----------------------------------------
Method :( Write this method on 'pagename.aspx.cs' page)
public void Transform_Method()
{
int CurrentPage = 1; // parameter to pass
if (this.Page.Request.QueryString["page"] != null)
{
CurrentPage = int.Parse(this.Page.Request.QueryString["page"]);
}
else
{
}
// Create the XsltArgumentList.
XsltArgumentList argList = new XsltArgumentList();
//passing the current page to page parameter of xslt
argList.AddParam("page", String.Empty, CurrentPage);
XmlReader reader = XmlReader.Create(MapPath("AllDeals.xml"));
XslCompiledTransform objXSLTransform = new XslCompiledTransform();
XsltSettings _setting = new XsltSettings(true, false);
objXSLTransform.Load(MapPath("AllDeals.xslt"), _setting, null);
StringBuilder htmlOutput = new StringBuilder();
TextWriter htmlWriter = new StringWriter(htmlOutput);
objXSLTransform.Transform(reader, argList, htmlWriter);
reader.Close();
Literal.Text = writer.ToString( ); // show the result on the Literal Control
}
---------------------------------
using System.Xml.Xsl;
using System.IO;
using System.Xml.XPath;
using System.Xml;
using System.Text;
-----------------------------------------
Method :( Write this method on 'pagename.aspx.cs' page)
public void Transform_Method()
{
int CurrentPage = 1; // parameter to pass
if (this.Page.Request.QueryString["page"] != null)
{
CurrentPage = int.Parse(this.Page.Request.QueryString["page"]);
}
else
{
}
// Create the XsltArgumentList.
XsltArgumentList argList = new XsltArgumentList();
//passing the current page to page parameter of xslt
argList.AddParam("page", String.Empty, CurrentPage);
XmlReader reader = XmlReader.Create(MapPath("AllDeals.xml"));
XslCompiledTransform objXSLTransform = new XslCompiledTransform();
XsltSettings _setting = new XsltSettings(true, false);
objXSLTransform.Load(MapPath("AllDeals.xslt"), _setting, null);
StringBuilder htmlOutput = new StringBuilder();
TextWriter htmlWriter = new StringWriter(htmlOutput);
objXSLTransform.Transform(reader, argList, htmlWriter);
reader.Close();
Literal.Text = writer.ToString( ); // show the result on the Literal Control
}
> You have 1 xml file i.e 'AllDeals.xml' & 1 xslt file i.e 'AllDeals.xslt'
> 'CurrentPage' is the Parameter which we are passing to the xslt 'AllDeals.xslt'
AllDeals.xslt :
--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!-- Items to display on a page -->
<xsl:variable name="pageSize" select="2"/>
<!-- Count the total no of records -->
<!--http://app.waft.in/api/xml.aspx?command=getalldeal¶m=&realtime=y-->
<xsl:variable name="Site">http://app.waft.in/</xsl:variable>
<xsl:variable name="Path" select="string(concat($Site,'api/xml.aspx?command=getalldeal&param=&realtime=y'))"/>
<xsl:variable name="recordCount" select="count(document($Path)/root/items/item)"/>
<!--current page parameter which is passed on from code behind through query string-->
<xsl:param name="page">
<!--<xsl:variable name="page" select="1"></xsl:variable>-->
<!--setting the page parameter-->
<xsl:choose>
<xsl:when test="$page > 1">
<xsl:value-of select="$page"></xsl:value-of>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:param>
<!-- the template for the xml doc starts here "root" is the root element in the xml doc-->
<xsl:template match="/">
<!-- for-each loop begins and we display based on the position of the item-->
<!--position function returns the position of the item in the for loop i.e
the first item in items/item will have position 1,the 2nd item will have position 2 and so on.
The ceiling function determines the page on which the item will be displayed.
Both of these are built in xslt functions-->
<xsl:for-each select="document($Path)/root/items/item">
<!-- now we calculate the page number of the item using ceiling function and compare it with the current page if the item lies on the current page
it is further processed to give its relevant xslt style otherwise we move on to the next item-->
<xsl:if test="ceiling(position() div $pageSize) = $page">
<div>
<a>
<xsl:value-of select="id"/>
</a>
<b>|</b>
<a>
<xsl:value-of select="title"/>
</a>
<b>|</b>
<a>
<xsl:value-of select="code"/>
</a>
</div>
</xsl:if>
</xsl:for-each>
<!--for each for items end here-->
<div>
<!-- Here we call the template that we created for paging and pass on the parameters as follows
the current page no,page size ie records to display on one page and total number of records-->
<xsl:call-template name="Paging">
<xsl:with-param name="currentPage" select="$page" />
<xsl:with-param name="rowsPerPage" select="$pageSize" />
<xsl:with-param name="totalRows" select="$recordCount" />
</xsl:call-template>
</div>
</xsl:template>
<!--main template ends-->
<!--Now here starts the logic for creating paging template
this template creates first and last page links ,five page buttons at max at a time as links and previous and next links-->
<xsl:template name="Paging">
<xsl:param name="currentPage" />
<xsl:param name="totalRows" />
<xsl:param name="rowsPerPage" />
<!--Here we calculate the total no of pages need to display the items-->
<xsl:variable name="totPages" select="ceiling($totalRows div $rowsPerPage)" />
<!--this condition checks the currentpage and displays the "first page" link if current page is not the 1st page-->
<xsl:if test="$currentPage > 1">
<a href="?page=1">
First
</a>
<a href="?page={$currentPage - 1}">
<img src="/images/backward.png" alt="prev" class="imageButton"></img>
</a>
</xsl:if>
<!--Now we set the start page ie the 1st page number link on the page-->
<xsl:variable name="pagStartPage">
<xsl:choose>
<xsl:when test="$currentPage <= 5">1</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$currentPage - 4"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--Now we call the template for generating page number links on the page-->
<!--we pass start page ,number of page links to display,the current page and the total no of pages as parameters to the template-->
<xsl:call-template name="PageNumbers">
<xsl:with-param name="i" select="$pagStartPage"/>
<xsl:with-param name="MaxPagelink" select="($pagStartPage+4)"/>
<xsl:with-param name="currentPage" select="$currentPage"/>
<xsl:with-param name="totPages" select="$totPages"/>
</xsl:call-template>
<!--this condition displays the next page link
here we check the current page is less than the total pages and display the next icon-->
<xsl:if test="$currentPage < $totPages" >
<a href="?page={$currentPage + 1}" >
<img src="/forward.png" alt="next" class="imageButton"></img>
</a>
</xsl:if>
<!-- Here we check if total records are not equal to zero followed by checking that the current page is not equal to the last page and display the last page link-->
<xsl:if test="$totalRows!= 0">
<xsl:if test="$currentPage != $totPages">
<a href="?page={$totPages}">
Last
</a>
</xsl:if>
</xsl:if>
</xsl:template>
<!--this template generates the page number links -->
<xsl:template name="PageNumbers">
<xsl:param name="i" />
<xsl:param name="MaxPagelink"/>
<xsl:param name="currentPage" />
<xsl:param name="totPages"/>
<xsl:choose>
<!--Here we check if the total pages is greater than max page link on the page else skip to otherwise tag-->
<xsl:when test="$totPages > $MaxPagelink">
<!--Here we check check if current page is less than or equal to max page link to display on the page-->
<xsl:if test="$i <= $MaxPagelink">
<xsl:choose>
<!--this condition displays an enabled number link if it is not the current page-->
<xsl:when test="$currentPage != $i">
<a href="?page={$i}">
<xsl:value-of select="$i"></xsl:value-of>
</a>
</xsl:when>
<!--if its the current page it disables the link-->
<xsl:otherwise>
<span>
<xsl:value-of select="$i"></xsl:value-of>
</span>
</xsl:otherwise>
</xsl:choose>
<!--Here we call the template in a loop until it "i" equals the maxpagenumber link to display-->
<xsl:call-template name="PageNumbers">
<xsl:with-param name="i" select="($i + 1)"></xsl:with-param>
<xsl:with-param name="MaxPagelink" select="$MaxPagelink"></xsl:with-param>
<xsl:with-param name="currentPage" select="$currentPage" ></xsl:with-param>
<xsl:with-param name="totPages" select="$totPages"> </xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:when>
<!-- This tag is called if the no of pages or total pages is less than the maxpage number link -->
<xsl:otherwise>
<!--Here we check if the no of pages are more than one,if yes than only display the page links-->
<xsl:if test="$totPages > 1">
<xsl:if test="$i <= $totPages">
<!--this condition displays an enabled number link if it is not the current page-->
<xsl:choose>
<xsl:when test="$currentPage != $i">
<a href="?page={$i}">
<xsl:value-of select="$i"></xsl:value-of>
</a>
</xsl:when>
<!--if its the current page it disables the link-->
<xsl:otherwise>
<span>
<xsl:value-of select="$i"></xsl:value-of>
</span>
</xsl:otherwise>
</xsl:choose>
<!--Here we call the template in a loop until it "i" equals the maxpagenumber link to display-->
<xsl:call-template name="PageNumbers">
<xsl:with-param name="i" select="($i + 1)"></xsl:with-param>
<xsl:with-param name="MaxPagelink" select="$MaxPagelink"></xsl:with-param>
<xsl:with-param name="currentPage" select="$currentPage" ></xsl:with-param>
<xsl:with-param name="totPages" select="$totPages"> </xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<!-- Items to display on a page -->
<xsl:variable name="pageSize" select="2"/>
<!-- Count the total no of records -->
<!--http://app.waft.in/api/xml.aspx?command=getalldeal¶m=&realtime=y-->
<xsl:variable name="Site">http://app.waft.in/</xsl:variable>
<xsl:variable name="Path" select="string(concat($Site,'api/xml.aspx?command=getalldeal&param=&realtime=y'))"/>
<xsl:variable name="recordCount" select="count(document($Path)/root/items/item)"/>
<!--current page parameter which is passed on from code behind through query string-->
<xsl:param name="page">
<!--<xsl:variable name="page" select="1"></xsl:variable>-->
<!--setting the page parameter-->
<xsl:choose>
<xsl:when test="$page > 1">
<xsl:value-of select="$page"></xsl:value-of>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:param>
<!-- the template for the xml doc starts here "root" is the root element in the xml doc-->
<xsl:template match="/">
<!-- for-each loop begins and we display based on the position of the item-->
<!--position function returns the position of the item in the for loop i.e
the first item in items/item will have position 1,the 2nd item will have position 2 and so on.
The ceiling function determines the page on which the item will be displayed.
Both of these are built in xslt functions-->
<xsl:for-each select="document($Path)/root/items/item">
<!-- now we calculate the page number of the item using ceiling function and compare it with the current page if the item lies on the current page
it is further processed to give its relevant xslt style otherwise we move on to the next item-->
<xsl:if test="ceiling(position() div $pageSize) = $page">
<div>
<a>
<xsl:value-of select="id"/>
</a>
<b>|</b>
<a>
<xsl:value-of select="title"/>
</a>
<b>|</b>
<a>
<xsl:value-of select="code"/>
</a>
</div>
</xsl:if>
</xsl:for-each>
<!--for each for items end here-->
<div>
<!-- Here we call the template that we created for paging and pass on the parameters as follows
the current page no,page size ie records to display on one page and total number of records-->
<xsl:call-template name="Paging">
<xsl:with-param name="currentPage" select="$page" />
<xsl:with-param name="rowsPerPage" select="$pageSize" />
<xsl:with-param name="totalRows" select="$recordCount" />
</xsl:call-template>
</div>
</xsl:template>
<!--main template ends-->
<!--Now here starts the logic for creating paging template
this template creates first and last page links ,five page buttons at max at a time as links and previous and next links-->
<xsl:template name="Paging">
<xsl:param name="currentPage" />
<xsl:param name="totalRows" />
<xsl:param name="rowsPerPage" />
<!--Here we calculate the total no of pages need to display the items-->
<xsl:variable name="totPages" select="ceiling($totalRows div $rowsPerPage)" />
<!--this condition checks the currentpage and displays the "first page" link if current page is not the 1st page-->
<xsl:if test="$currentPage > 1">
<a href="?page=1">
First
</a>
<a href="?page={$currentPage - 1}">
<img src="/images/backward.png" alt="prev" class="imageButton"></img>
</a>
</xsl:if>
<!--Now we set the start page ie the 1st page number link on the page-->
<xsl:variable name="pagStartPage">
<xsl:choose>
<xsl:when test="$currentPage <= 5">1</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$currentPage - 4"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--Now we call the template for generating page number links on the page-->
<!--we pass start page ,number of page links to display,the current page and the total no of pages as parameters to the template-->
<xsl:call-template name="PageNumbers">
<xsl:with-param name="i" select="$pagStartPage"/>
<xsl:with-param name="MaxPagelink" select="($pagStartPage+4)"/>
<xsl:with-param name="currentPage" select="$currentPage"/>
<xsl:with-param name="totPages" select="$totPages"/>
</xsl:call-template>
<!--this condition displays the next page link
here we check the current page is less than the total pages and display the next icon-->
<xsl:if test="$currentPage < $totPages" >
<a href="?page={$currentPage + 1}" >
<img src="/forward.png" alt="next" class="imageButton"></img>
</a>
</xsl:if>
<!-- Here we check if total records are not equal to zero followed by checking that the current page is not equal to the last page and display the last page link-->
<xsl:if test="$totalRows!= 0">
<xsl:if test="$currentPage != $totPages">
<a href="?page={$totPages}">
Last
</a>
</xsl:if>
</xsl:if>
</xsl:template>
<!--this template generates the page number links -->
<xsl:template name="PageNumbers">
<xsl:param name="i" />
<xsl:param name="MaxPagelink"/>
<xsl:param name="currentPage" />
<xsl:param name="totPages"/>
<xsl:choose>
<!--Here we check if the total pages is greater than max page link on the page else skip to otherwise tag-->
<xsl:when test="$totPages > $MaxPagelink">
<!--Here we check check if current page is less than or equal to max page link to display on the page-->
<xsl:if test="$i <= $MaxPagelink">
<xsl:choose>
<!--this condition displays an enabled number link if it is not the current page-->
<xsl:when test="$currentPage != $i">
<a href="?page={$i}">
<xsl:value-of select="$i"></xsl:value-of>
</a>
</xsl:when>
<!--if its the current page it disables the link-->
<xsl:otherwise>
<span>
<xsl:value-of select="$i"></xsl:value-of>
</span>
</xsl:otherwise>
</xsl:choose>
<!--Here we call the template in a loop until it "i" equals the maxpagenumber link to display-->
<xsl:call-template name="PageNumbers">
<xsl:with-param name="i" select="($i + 1)"></xsl:with-param>
<xsl:with-param name="MaxPagelink" select="$MaxPagelink"></xsl:with-param>
<xsl:with-param name="currentPage" select="$currentPage" ></xsl:with-param>
<xsl:with-param name="totPages" select="$totPages"> </xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:when>
<!-- This tag is called if the no of pages or total pages is less than the maxpage number link -->
<xsl:otherwise>
<!--Here we check if the no of pages are more than one,if yes than only display the page links-->
<xsl:if test="$totPages > 1">
<xsl:if test="$i <= $totPages">
<!--this condition displays an enabled number link if it is not the current page-->
<xsl:choose>
<xsl:when test="$currentPage != $i">
<a href="?page={$i}">
<xsl:value-of select="$i"></xsl:value-of>
</a>
</xsl:when>
<!--if its the current page it disables the link-->
<xsl:otherwise>
<span>
<xsl:value-of select="$i"></xsl:value-of>
</span>
</xsl:otherwise>
</xsl:choose>
<!--Here we call the template in a loop until it "i" equals the maxpagenumber link to display-->
<xsl:call-template name="PageNumbers">
<xsl:with-param name="i" select="($i + 1)"></xsl:with-param>
<xsl:with-param name="MaxPagelink" select="$MaxPagelink"></xsl:with-param>
<xsl:with-param name="currentPage" select="$currentPage" ></xsl:with-param>
<xsl:with-param name="totPages" select="$totPages"> </xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
No comments:
Post a Comment