播客 > 玩代码  >  XSLT 分页 模板 公开  | 登录  | RSS订阅地址  | Code平台

XSLT 分页 模板 公开

   我之前写了几篇关于XSLT的文字,但是没什么实例写出来,就一直搁置下了,今天就把我的一个分页模板公开,以便有益于读者同学们的抄袭。
   Tags:分页 XML XSLT
   先看看显示效果: XSLT教程 注意看那个分页的。就是这个东西。
  这个东西所需要注意的几个元素是:

  1:页数信息,建议一个XML节点。


  例如我搜索完毕以后,生成的XML节点信息如下:

<Page AllRecordset="100" PageSize="12" ThisPage="3"/>

表示的是所获得了100条数据,每页12条,目前第三页。
 
 2:还有一个就是URL参数,有中文的话你就最好预先格式化处理下,负责XML转换编码的时候给你编码成不确定的字符。
这个,一般作为XSL文件的系统变量设置,例如:

System.Xml.Xsl.XsltArgumentList Xargs=new System.Xml.Xsl.XsltArgumentList();
Xargs.AddParam("SelectUri", "", "index.aspx?q="+(Q)+"&page=");//

在相关的XSL文件中:
<xsl:param name="SelectUri"/>


看主XSLT文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
     <!ENTITY raquo "&#187;">
     <!ENTITY nbsp "&#160;">
     <!ENTITY rsaquo "&#8250;">
     <!ENTITY lsaquo "&#8249;">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" media-type="text/xml"   encoding="utf-8" indent="no"/>
<xsl:param name="SelectUri"/>
<xsl:template name="page">
    <xsl:param name="ThisPage"/>
    <xsl:param name="AllRecordset"/>
    <xsl:param name="PageSize"/>
    <xsl:variable name="PageCount" select="ceiling($AllRecordset div $PageSize)"/>
    <xsl:variable name="Loopi" select="$ThisPage"/>
    <span class="Recordset">All Get <xsl:value-of select="$AllRecordset"/></span>
    <span class="PageNode"><xsl:value-of select="$ThisPage"/>/<xsl:value-of select="$PageCount"/></span>

    <xsl:if test="number($ThisPage)&gt;1">
    <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$SelectUri" disable-output-escaping="no"/>1</xsl:attribute>|&lt;</xsl:element>
    <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$SelectUri" disable-output-escaping="no"/><xsl:value-of select="number($ThisPage)-1"/></xsl:attribute>&lt;&lt;</xsl:element>
    </xsl:if>

    <xsl:choose>
    <xsl:when test="number($PageCount)&lt;=5">
       <xsl:call-template name="NodePage">
        <xsl:with-param name="PageMax"><xsl:value-of select="$PageCount"/></xsl:with-param>
        <xsl:with-param name="NodeValue">1</xsl:with-param>
        <xsl:with-param name="ThisPage"><xsl:value-of select="$ThisPage"/></xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="number($ThisPage)-2 &lt;=0">
        <xsl:call-template name="NodePage">
            <xsl:with-param name="PageMax">5</xsl:with-param>
            <xsl:with-param name="NodeValue">1</xsl:with-param>
            <xsl:with-param name="ThisPage"><xsl:value-of select="$ThisPage"/></xsl:with-param>
        </xsl:call-template>
    </xsl:when>
    <xsl:when test="number($ThisPage)+2 &gt;=number($PageCount)">
        <xsl:call-template name="NodePage">
            <xsl:with-param name="PageMax"><xsl:value-of select="$PageCount"/></xsl:with-param>
            <xsl:with-param name="NodeValue"><xsl:value-of select="number($PageCount)-5"/></xsl:with-param>
            <xsl:with-param name="ThisPage"><xsl:value-of select="$ThisPage"/></xsl:with-param>
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:call-template name="NodePage">
            <xsl:with-param name="PageMax"><xsl:value-of select="number($Loopi+2)"/></xsl:with-param>
            <xsl:with-param name="NodeValue"><xsl:value-of select="number($ThisPage)-2"/></xsl:with-param>
            <xsl:with-param name="ThisPage"><xsl:value-of select="$ThisPage"/></xsl:with-param>
        </xsl:call-template>
    </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="number($ThisPage)&lt;number($PageCount)">
    <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$SelectUri" disable-output-escaping="no"/><xsl:value-of select="$ThisPage+1"/></xsl:attribute>&gt;&gt;</xsl:element>
    <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$SelectUri" disable-output-escaping="no"/><xsl:value-of select="$PageCount" /></xsl:attribute>&gt;|</xsl:element>
    </xsl:if>

</xsl:template>
<xsl:template name="NodePage">
    <xsl:param name="PageMax"/>
    <xsl:param name="NodeValue"/>
    <xsl:param name="ThisPage"/>
    <xsl:variable name="i" select="$NodeValue"/>
    <xsl:choose>
        <xsl:when test="$PageMax &gt;=$i">
            <xsl:element name="a">
                <xsl:attribute name="href"><xsl:value-of select="$SelectUri" disable-output-escaping="no"/><xsl:value-of select="$i"/></xsl:attribute>
                <xsl:if test="$ThisPage=$i">
                    <xsl:attribute name="class">Selected</xsl:attribute>
                </xsl:if>
                <xsl:value-of select="$i"/>
            </xsl:element>
            <xsl:variable name="j" select="$i + 1"/>
            <xsl:call-template name="NodePage">
                <xsl:with-param name="PageMax"><xsl:value-of select="$PageMax"/></xsl:with-param>
                <xsl:with-param name="NodeValue"><xsl:value-of select="$j"/></xsl:with-param>
                <xsl:with-param name="ThisPage"><xsl:value-of select="$ThisPage"/></xsl:with-param>
            </xsl:call-template>
        </xsl:when>

    </xsl:choose>
</xsl:template>
</xsl:stylesheet>


我一下子全部列出来。

使用的时候:
    <div class="Pager">
        <xsl:call-template name="page">
        <xsl:with-param name="ThisPage"><xsl:value-of select="number(ResultBox/Result/Page/@ThisPage)"/></xsl:with-param>
        <xsl:with-param name="AllRecordset"><xsl:value-of select="number(ResultBox/Result/Page/@AllRecordset)"/></xsl:with-param>
        <xsl:with-param name="PageSize"><xsl:value-of select="number(ResultBox/Result/Page/@PageSize)"/></xsl:with-param>
        </xsl:call-template>
    </div>

当然之前要:
<xsl:include href="MovePage.xsl"/>


主要其中有两点:
1:转换URL
2:循环判断

关于XSLT的更多资料,本博客也写了好几篇,读者可以用搜索搜下。
天气:大雨,ccdot发表于2008-7-22 17:14:58,阅读了528次,共有个1回复.

我最近才刚开始学,这篇文章的代码我没大看懂,不过比起其他分页教程要讲的详细,非常不错。

yanglei post in 2008-7-31 17:25:44 #1  
  1. 想要转载我文章的人滚远远的,能想多远,就滚多远。
  2. 不要提交任何带有网址URL信息的评论.
  3. 需要更多信息?请使用站内搜索,郁闷了?听听我在听什么吧!
用户名:*验证:看不清楚请点击刷新验证码*
内容: