仅用xsl和asp实现分页功能

注意事项:
※本文代码可能有一些多余部分未去掉,请在阅读时忽略。
一些外部include进来的文件这里就不贴上来了。
※小乙写xsl也不久,很多语句都不会使用,有些地方写得比较罗嗦,
如果您有更好的分页代码,请多多拿来交流。
※适用于:用asp load进来xml代码,然后用此xsl文件进行分页处理。
※[2001.2.19]
------------------------------------
asp文件大致结构:

1@ Language=VBScript 
 1   
 2cc=server.MapPath("trans.xml")   
 3set source=server.CreateObject("msxml2.domdocument")   
 4source.async=false   
 5source.load(cc)   
 6  
 7xslfile=server.MapPath("index.xsl")   
 8set style=server.CreateObject("msxml2.domdocument")   
 9style.async=false   
10style.load(xslfile)   
11  
12'Response.write source.transformNode(style)   
13Response.write gb_html(source.transformNode(style))   
14Response.End   

------------------------------------load进来的xml数据是这样的:

1<root>
2<function>
3<po>里面的标签在后面的xsl文件里被"<xsl:for-each>"</xsl:for-each></po>
4<po>……………………</po>
5<po>……………………</po>
6<po>……………………</po>
7</function>
8</root>

------------------------------------
xsl文件的内容:

  1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2<xsl:include href="include/ydzhongxin.xsl"></xsl:include><!-- 嵌入头模板,尾模板 -->
  3<xsl:param name="yd">7</xsl:param><!-- 调用二级导航条所用参数 -->
  4<xsl:param name="page"> <xsl:value-of select="count(//PO)"></xsl:value-of></xsl:param>
  5<!-- 定义根模板 -->
  6<xsl:template match="/">
  7<html>
  8<head>
  9<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
 10<link href="include/style.css" rel="stylesheet" type="text/css"/>
 11<title>结果列表</title>
 12</head>
 13<body leftmargin="0" topmargin="0">
 14<xsl:call-template name="ydtitle"></xsl:call-template>
 15<div align="center">
 16<xsl:apply-templates select="root/function"></xsl:apply-templates>
 17<!-- 匹配function模板 -->
 18</div>
 19<xsl:call-template name="end"></xsl:call-template>
 20</body>
 21</html>
 22</xsl:template>
 23<!-- 定义function模板 -->
 24<xsl:template match="function">
 25<!-- ---------------翻页链接开始----------- -->
 26<xsl:variable name="pagesize">5</xsl:variable><!-- 是分页参数 -->
 27<xsl:choose>
 28<xsl:when test="/root/session/page[text()!='']">
 29<!-- 进入一级choose的一个when条件分支!!!!!   
 30-------------进入此分支,证明用户已有翻页操作-------------- -->
 31<xsl:variable name="page"><xsl:value-of select="/root/session/page"></xsl:value-of></xsl:variable>
 32<table border="0" cellpadding="2" cellspacing="0" width="630">
 33<tr>
 34<td align="right">
 35<!-- 进入二级choose!!! -->
 36<xsl:choose>
 37<!-- ①id小于等于0的情况,显示最后一页。-->
 38<xsl:when test="$pid&lt;1">
 39<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 40(//PO)"></xsl:value-of></xsl:attribute>   
 41[ 首 ]</a>
 42<a title="前一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$size*2"></xsl:value-of></xsl:attribute>[ &lt;&lt;&lt; ] </a>
 43<a title="后一页">[ &gt;&gt;&gt; ] </a>
 44<a>[ 尾 ]</a>
 45</xsl:when>
 46<!-- ②id位于[0~pagesize]之间的情况,前页正常,后页无。 -->
 47<xsl:when test="$pid&lt;($size + 1) and $pid&gt;0">
 48<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 49(//PO)"></xsl:value-of></xsl:attribute>   
 50[ 首 ]</a>
 51<a title="前一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$pid+$size"></xsl:value-of></xsl:attribute>[ &lt;&lt;&lt; ] </a>
 52<a title="后一页">[ &gt;&gt;&gt; ] </a>
 53<a>[ 尾 ]</a>
 54</xsl:when>
 55<!-- ③id位于[pagesize~count]之间的情况,前页无,后页正常。 -->
 56<xsl:when test="$pid&lt;count(//PO) and $pid&gt;(count(//PO)-$size)">
 57<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 58(//PO)"></xsl:value-of></xsl:attribute>   
 59[ 首 ]</a>
 60<a title="前一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 61(//PO)"></xsl:value-of></xsl:attribute>[ &lt;&lt;&lt; ] </a>
 62<a title="后一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$pid -   
 63$size"></xsl:value-of></xsl:attribute>[ &gt;&gt;&gt; ] </a>
 64<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$size"></xsl:value-of></xsl:attribute>   
 65[ 尾 ]</a>
 66</xsl:when>
 67<!-- ④id等于count的情况,显示首页。 -->
 68<xsl:when test="$pid=count(//PO)">
 69<a>[ 首 ]</a>
 70<a title="前一页">[ &lt;&lt;&lt; ] </a>
 71<a title="后一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 72(//PO)-$size"></xsl:value-of></xsl:attribute>[ &gt;&gt;&gt; ] </a>
 73<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$size"></xsl:value-of></xsl:attribute>   
 74[ 尾 ]</a>
 75</xsl:when>
 76<!-- ⑤id大于count的情况,显示首页。 -->
 77<xsl:when test="$pid&gt;count(//PO)">
 78<a>[ 首 ]</a>
 79<a title="前一页">[ &lt;&lt;&lt; ] </a>
 80<a title="后一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 81(//PO)-$size"></xsl:value-of></xsl:attribute>[ &gt;&gt;&gt; ] </a>
 82<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$size"></xsl:value-of></xsl:attribute>   
 83[ 尾 ]</a>
 84</xsl:when>
 85<!-- 正常情况 -->
 86<xsl:otherwise>
 87<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="count   
 88(//PO)"></xsl:value-of></xsl:attribute>   
 89[ 首 ]</a>
 90<a title="前一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$pid +   
 91$size"></xsl:value-of></xsl:attribute>[ &lt;&lt;&lt; ] </a>
 92<a title="后一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$pid -   
 93$size"></xsl:value-of></xsl:attribute>[ &gt;&gt;&gt; ] </a>
 94<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$size"></xsl:value-of></xsl:attribute>   
 95[ 尾 ]</a>
 96</xsl:otherwise>
 97</xsl:choose>
 98<!-- ---------------------------------------- -->
 99</td>
100</tr>
101</table>
102<!-- ---------遍历符合要求的PO结点------------- -->
103<xsl:for-each select="PO[position()&lt;=$pid and position()&gt;($pid - $size)]">
104<xsl:sort data-type="number" order="descending" select="PO_ID"></xsl:sort>
105<xsl:call-template name="PO"></xsl:call-template>
106</xsl:for-each>
107<!-- 退出一级choose的一个when条件分支!!!!! -->
108</xsl:when>
109<!-- ------------------用户直接进入的状态------------------ -->
110<xsl:otherwise>
111<!-- 进入一级choose的另一个when条件分支!!!!! -->
112<table border="0" cellpadding="2" cellspacing="0" width="630">
113<tr><td align="right">
114<a>[ 首 ]</a>
115<a title="前一页">[ &lt;&lt;&lt; ] </a>
116<a title="后一页"><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$pid -   
117$size"></xsl:value-of></xsl:attribute>[ &gt;&gt;&gt; ] </a>
118<a><xsl:attribute name="href">search_jieguo.asp?id=<xsl:value-of select="$size"></xsl:value-of></xsl:attribute>   
119[ 尾 ]</a>
120</td></tr>
121</table>
122<xsl:for-each select="PO[position()&lt;=$pid and position()&gt;($pid - $size)]">
123<xsl:sort data-type="number" order="descending" select="PO_ID"></xsl:sort>
124<xsl:call-template name="PO"></xsl:call-template>
125</xsl:for-each>
126<!-- 退出一级choose的另一个when条件分支!!!!! -->
127</xsl:otherwise>
128</xsl:choose>
129<!-- --------------翻页链接到此结束----------- -->
130<xsl:if test="count(//PO)=0">
131<div align="center"><b>
132<img align="absmiddle" src="images/msg2.gif"/>   
133</b><font color="#CC0000" face="楷体CS" size="3"><b>   
134没有符合当前条件的订单</b></font>
135<a><xsl:attribute name="href">lkxx.asp?po_id=<xsl:value-of select="PO_ID"></xsl:value-of></xsl:attribute></a>
136</div>   
137&gt;
138
139   
140<input onclick="location.href='search.asp'" type="button" value="重新输入条件查询"/>
141</xsl:if>
142</xsl:template>
143<!-- ------------------------------------------>
144<xsl:template name="PO">
145<table border="1" cellpadding="2" cellspacing="0" width="100%">
146<tr>
147<td nowrap="nowrap" width="70"> 号码</td>
148<td nowrap="nowrap" width="110"> 名称</td>
149<td nowrap="nowrap" width="110"> 日期</td>
150<td nowrap="nowrap" width="110"> 人员</td>
151</tr>
152<tr>
153<td nowrap="nowrap"> <xsl:value-of select="num"></xsl:value-of></td>
154<td nowrap="nowrap"> <xsl:value-of select="username"></xsl:value-of></td>
155<td nowrap="nowrap"> <xsl:value-of select="dt"></xsl:value-of></td>
156<td nowrap="nowrap"> <xsl:value-of select="men"></xsl:value-of></td>
157</tr>
158</table>
159</xsl:template>
160</xsl:stylesheet>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus