例一:
关于XSLT及XML的介绍就不再多说,这里关注的是如何使用XSLT分页显示XML中的数据,并可以针对每一列对全部数据进行重新排序.
假设有下面的XML数据,需要使用XSLT分页显示XML数据,并可针对每一列对所有数据进行重新排序,应该如何处理呢?
XML数据如下:
1
<?xml version="1.0" encoding="gb2312"?>
2
<AllUsers xmlns:dt="urn:schemas-microsoft-com:datatypes">
3
<User>
4
<UserID>01</UserID>
5
<UserName>OhYeah</UserName>
6
<UserEmail>Oh@126.com</UserEmail>
7
</User>
8
<User>
9
<UserID>02</UserID>
10
<UserName>杨过</UserName>
11
<UserEmail>yg@21.com</UserEmail>
12
</User>
13
<User>
14
<UserID>03</UserID>
15
<UserName>真宝玉</UserName>
16
<UserEmail>xyz@msn.com</UserEmail>
17
</User>
18
<User>
19
<UserID>04</UserID>
20
<UserName>天堂</UserName>
21
<UserEmail>tt@163.net</UserEmail>
22
</User>
23
<User>
24
<UserID>05</UserID>
25
<UserName>草原</UserName>
26
<UserEmail>yt@sina.com</UserEmail>
27
</User>
28
</AllUsers>
29
恐怕最容易想到的就是XSLT中的for-each元素.
1
<xsl:param name="PageNo">1</xsl:param>
2
<xsl:param name="FuncName">goPage</xsl:param>
3
<xsl:param name="Count">4</xsl:param>
4
<table width="800" border="0" cellspacing="2" cellpadding="2">
5
<tr>
6
<th><xsl:attribute name="onClick">sortBy('UserID')</xsl:attribute>RecordNo</th>
7
<th><xsl:attribute name="onClick">sortBy(' UserName')</xsl:attribute>FieldA</th>
8
<th><xsl:attribute name="onClick">sortBy(' UserEmail')</xsl:attribute>Name</th>
9
</tr>
10
<xsl:for-each select="RecordSet[(position() <= $PageNo*$Count) and (position() > (number($PageNo)-1)*$Count)]">
11
<xsl:sort select="UserID"/>
12
<tr>
13
<td><xsl:value-of select="'UserID'"/></td>
14
<td><xsl:value-of select=" UserName"/></td>
15
<td><xsl:value-of select=" UserEmail"/></td>
16
</tr>
17
</xsl:for-each>
18
</table>
19
页码数发生变化时,通过JavaScript指定for-each中的$PageNo变量.
重新排序时,通过JavaScript指定sort元素的select属性.
但这里就有一个问题:这里点击列名重新排序时,只是对当前页的数据进行了重新排序(原因是先用for-each取出的当前页的数据,然后才进行排序的),咱们本来的目标应该是对所有数据重新排序的.
解决这个问题的过程中,孟子E章曾经给我一篇他网站上的文章,解决了这个问题.但网页使用的是VBScript,并且不能正确运行.为此,浪费了不少时间(-_-!!!,太懒了,就希望借用成功的代码,遇到不正确的代码就不愿自己去看.).不过最终总算搞明白了.解决方案就是先用一个XSLT对所有数据重新排序后生成XML,再对重新排序后的XML进行分页操作.
用于对XML排序并生成XML的XSLT如下:
1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2
<xsl:template match="Table">
3
<?xml version="1.0" encoding="iso-8859-1"?>
4
< AllUsers >
5
<xsl:for-each select="RecordSet">
6
<xsl:sort order="ascending" data-type="text" select="Name"/>
7
<User>
8
<UserID><xsl:value-of select="RecordNo" /></UserID>
9
<UserName><xsl:value-of select="FieldA" /></UserName>
10
<UserEmail><xsl:value-of select="Name" /></UserEmail>
11
</User>
12
</xsl:for-each>
13
</ AllUsers >
14
</xsl:template>
15
</xsl:stylesheet>
例子二:
posted on 2008-06-04 16:54
acerbic coffee 阅读(700)
评论(0) 编辑 收藏 所属分类:
个人笔记