网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.ASP整合的一个SQL语句类.
.用isapi rewrite实现asp的HTML静.
.利用OWC服务器端组件动态生成图表.
.浅谈ASP中Web页面间的数据传递.
.用XML数据岛解决用户界面问题 - 3.
.asp网页提升速度方法5则.
.WAP版的手机号码所在地查询.
.在ASP.NET中使用.NET组件.
.remote script文档(转载自微软)(.
.使用ASP和Word进行服务器端拼写检.
.如何增强ASP程序性能(3).
.将asp页面转换成htm页面.
.巧用in关键字实现数据的批量删除.
.在ASP+中使用Cookie.
.在ASP处理程序时显示进度.
.浅谈ASP编程的思路与纠错.
.提高ASP性能的最佳选择(续二).
.如何在pb中创建COM组件,并在asp.
.一个比较实用的asp函数集合类 .
.关于Adodb.Stream的使用说明.

数据排序及如何动态排序

发表日期:2001-2-12


数据排序及如何动态排序

//Belltree
//http://www.lurer.net/

//初学XML,错误之处多多,各路高手多多指正

在<xsl:for-each select="//item" order-by="text()">及<xsl:apply-templates select="//item"/>中都可以看到
order-by属性,该属性可以对选出来的节点按照order-by的值进行排序.

<singer>
<title>Christina Aguilera</title>
<songs>
<item id="0" href="genieinabottle.christina.xml">Genie in a bottle</item>
<item id="1" href="whatagirlwants.christina.xml">What a girl wants</item>
<item id="2" href="iturntoyou.christina.xml">I turn to you</item>
<item id="5" href="soemotional.christina.xml">So emotional</item>
<item id="4" href="comeonover.christina.xml">Come on over</item>
<item id="3" href="reflection.christina.xml">Reflection</item>
<item id="6" href="lovefor.christina.xml">Love for all seasons</item>
<item id="7" href="somebody.christina.xml">Somebody's somebody</item>
<item id="10" href="puturhands.christina.xml">When you put your hands on me</item>
<item id="9" href="blessed.christina.xml">Blessed</item>
<item id="8" href="lovefindaway.christina.xml">Love will find a way</item>
<item id="11" href="obvious.christina.xml">obvious</item>
</songs>
</singer>

在这个例子中,如果我们需要一个按照歌名进行排序的列表,可以使用如下XSL:

<xsl:for-each select="//item" order-by="text()">
<a><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute><xsl:value-of /></a>
<br/>
</xsl:for-each>

这样就按照每个item节点的值进行了排序,还可以使用id属性来排序,只要将order-by="text()"改为oder-by="@id"即可.

但如果我们需要让用户自己选择如何排序,甚至是不排序,即按照原始顺序.

这时就该让script和XML DOM上场了,在DOM中,有一系列的Methods和Attributes让你设置,你可以重新生成一棵树,我们就可
以利用这些东东将order-by属性的值改掉,然后再重新利用这个XSL节点对你需要的节点数据重新生成一棵树,这棵树是排序
了的,注意,order-by的值可不是原来的了,是你的新值.你甚至可以将order-by属性从XSL节点属性中去掉,这样生成的树就
是按照原始顺序了.

看看相应的XML DOM Methods:
selectSingleNode   返回单个节点
setAttribute       设置属性值,如果属性不存在,创建它并设置值
removeAttribute    移去属性
transformNode      使用相应的XSL stylesheet对该节点及其字节点进行处理后,返回一个结果树

最开始,我们要将XSL中的xsl:for-each节点选出来,并将它赋予一个变量s,好对它进行处理:
var s = document.XSLDocument.selectSingleNode("//xsl:for-each")

然后,对它的属性order-by的值从新设置:
setAttribute("order-by",key);  //key为一个变量,可以为id,text()

或者,将其删去:
removeAttribute("order-by");

哈哈,很简单吧

我们现在来看看源树中需要排序的部分,是singer/songs节点中的每个item节点,不需要选择整个树,只要singer/songs就可
以了

var xmldoc = document.XMLDocument.selectSingleNode("singer/songs");

将整个XSL树应用该节点:

divItems.innerHTML = xmldoc.transformNode(document.XSLDocument);  //错误来了

是不是要对这个节点应用整个XSL树呢?当然不必,这样也会带来错误,应为结果必须显示在某个地方,我们回头来看一看:
<xsl:template match="/">
<div id="divItems">
<div id="in">

<xsl:for-each select="//item" order-by="id">
<a><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute><xsl:value-of /></a>
<br/>
</xsl:for-each>

</div>
</div>
</xsl:template>
我们在<xsl:for-each>的用<div>容器包住,为什么要用两个<div>呢,这个后面说明,先来看看transformNode,应用整个XSL
树会带来什么后果,这样又会重新生成一个<div id="divItems">...</div>,就会导致两个同id的div,结果是无法运行.

我们只需要选<div id="in">这个节点就够了.

var xsldoc = document.XSLDocument.selectSingleNode("//div[@id='in']");

然后将xsldoc应用到xmldoc上就成了

divItems.innerHTML = xmldoc.transformNode(xsldoc);

两个div还是有用的吧,第一个是一个显示结果的容器,第二个每次都包含在结果树中,如果没有<div id="in">直接选<div
id="divItems">就会出现和应用整个XSL树一样的错误.

最后,还是看看完整的:(具体效果请看http://go8.163.com/~belltree/test.xml)
XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>

<singer>
<title>Christina Aguilera</title>
<songs>
<item id="0" href="genieinabottle.christina.xml">Genie in a bottle</item>
<item id="1" href="whatagirlwants.christina.xml">What a girl wants</item>
<item id="2" href="iturntoyou.christina.xml">I turn to you</item>
<item id="3" href="soemotional.christina.xml">So emotional</item>
<item id="4" href="comeonover.christina.xml">Come on over</item>
<item id="5" href="reflection.christina.xml">Reflection</item>
<item id="6" href="lovefor.christina.xml">Love for all seasons</item>
<item id="7" href="somebody.christina.xml">Somebody's somebody</item>
<item id="8" href="puturhands.christina.xml">When you put your hands on me</item>
<item id="9" href="blessed.christina.xml">Blessed</item>
<item id="10" href="lovefindaway.christina.xml">Love will find a way</item>
<item id="11" href="obvious.christina.xml">obvious</item>
</songs>
</singer>

XSL:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">

<html>
<script language="javascript">
<xsl:comment>
function sort(key) {
  // Find the "for-each" attributes in the style sheet.
  var s = document.XSLDocument.selectSingleNode("//xsl:for-each");
                                            
  if (key=="")
  s.removeAttribute("order-by");
  else
  s.setAttribute("order-by",key);
  
  // Find the subset of the document we need to update.
  var xmldoc = document.XMLDocument.selectSingleNode("singer/songs");
  var xsldoc = document.XSLDocument.selectSingleNode("//div[@id='in']");
  
  // Apply the style sheet to the subset, and update the display.
  divItems.innerHTML = xmldoc.transformNode(xsldoc);
}


</xsl:comment>
</script>
<body>
<table border="1" cellspacing="0" cellpadding="1">
<tr><td>
<div id="divItems">
<div id="in">

<xsl:for-each select="//item" order-by="@id">
<a><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute><xsl:value-of /></a>
<br/>
</xsl:for-each>

</div>
</div>
</td><td>
<input type="button" value=" Text " onClick="sort('text()')"/>
<input type="button" value=" @id " onClick="sort('@id')"/>
<input type="button" value="Origin" onClick="sort('')"/>
</td></tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
上一篇:连接各种数据库的代码的总结 人气:10165
下一篇:将HTML表单数据存储为XML格式 人气:9199
浏览全部的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐