此方案使用dom中内置的XSLTemplate对象;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name=ProgId content=VisualStudio.HTML>
<meta name=Originator content="Microsoft Visual Studio .NET 7.1">
</head>
<body>
<div id=oDiv></div>
<script language=javascript>
var xslt = new ActiveXObject("Msxml2.XSLTemplate");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
var xslProc;
xslDoc.async = false;
xslDoc.load("csdn0030.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = false;
xmlDoc.load("csdn0030.xml");
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
function _trans(){
xslProc.transform();
oDiv.innerHTML=xslProc.output;
}
function _onchange(obj,key){
xslProc.addParameter(key,obj[obj.selectedIndex].value);
_trans();
}
_trans();
</script>
排序字段:
<select id='key' οnchange="_onchange(this,'key')">
<option value=id>id</option>
<option value=name>name</option>
</select>
排序类型:
<select id="Select1" οnchange="_onchange(this,'order')" NAME="Select1">
<option value=descending>descending</option>
<option value=ascending>ascending</option>
</select>
</body>
</html>
csdn0030.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<user>
<id>1</id>
<name>ssm</name>
</user>
<user>
<id>4</id>
<name>assm</name>
</user>
<user>
<id>3</id>
<name>cssm</name>
</user>
<user>
<id>2</id>
<name>bssm</name>
</user>
<user>
<id>5</id>
<name>essm</name>
</user>
</root>
csdn0030.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="key">id</xsl:param>
<xsl:param name="order">descending</xsl:param>
<xsl:template match="/">
<xsl:for-each select="//user">
<xsl:sort select="*[name()=$key]" order="{$order}"/>
<xsl:value-of select="id"/><br/>
<xsl:value-of select="name"/>
<hr/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>