在 xsl 中做用户筛选的问题
最近在做 xsl 解析, 在 xml 中通过 href 指定 xsl, 这样打开 xml 文件时, 可以通过 href 指定的 xls 对 xml 做一个友好展示
现在的问题是, 准备在 xsl 中增加一个用户筛选, 允许用户对要看到的数据做进一步筛选
xml 示例如下 (filter.xml)
<?xml-stylesheet type="text/xsl" href="filter.xsl"?>
<root>
<group name="group1">
<item id="1">item 1</item>
<item id="2">item 2</item>
</group>
<group name="group2">
<item id="1">item a</item>
<item id="2">item b</item>
</group>
</root>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/root">
<xsl:param name="GroupName" select ="(group/@name)[1]"/>
<html>
<head>
<meta content="text/html" http-equiv="Content-Type"></meta>
<script language="javascript" name="Filter">
<![CDATA[
function filter_group(sName) {
var iXml = document.XMLDocument;
var iXsl = document.XSLDocument;
//iXsl.addParameter("GroupName", sName); //
var iPara = iXsl.selectSingleNode('//xsl:param[@name="GroupName"]');
alert(iPara.text);
var iHtml = iXml.transformNode(iXsl);
document.write(iHtml);
}
]]>
</script>
</head>
<body>
<table>
<tr>
<td>
<form style="BORDER: 0">
<select id="frm_Server">
<option value="" selected="1"><![CDATA[<- Please Select ->]]></option>
<xsl:for-each select ="group/@name">
<xsl:sort select ="."/>
<option>
<xsl:attribute name ="value">
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:value-of select="."/>
</option>
</xsl:for-each>
<option value="*"><![CDATA[<- ALL ->]]></option>
</select>
<button onClick="filter_group(this.form.frm_Server.value)">Query</button>
</form>
</td>
</tr>
<tr>
<td>
<xsl:apply-templates select="group[@name=$GroupName or $GroupName = '*']"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="group">
<table>
<thead>
<tr>
<th>id</th>
<th>value</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="item">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>