请教各位:HTML文件中用DOM的addparameter方法,为什么不能将值传递到XSL文件中的变量呢?(50分)

  • 主题发起人 主题发起人 Mark Lai
  • 开始时间 开始时间
M

Mark Lai

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手:
小弟近日新学XML.现在要将一个XML文件用在HTML文件中动态的显示出来。
XML文件的结构是
<root>
<menu name="主菜单名">
<menu name="菜单名">
……一些其它元素
</menu>
<command name="菜单项名">
……一些其它元素
</command>
……
……
</menu>
<menu name="主菜单名">
<menu name="菜单名">
……一些其它元素
</menu>
<command name="菜单项名">
……一些其它元素
</command>
</menu>
……
……
……
……
</root>
我创建了两个XSL文件(原先是想只做一个,但是还不熟悉XSL,所以做成了两个),menuXSl.xsl
——————————
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="menuindex">1</xsl:variable>
<xsl:variable name="menuname"/>
<!--mainMenu-->
<xsl:template match="/">
<table width="100%" border="1" bgcolor="#CCCCCC">
<tbody>
<tr>
<xsl:for-each select="//menu[parent::root]">
<th onclick="showmenu('{@name}','1')">
<xsl:value-of select="@name"/>
</th>
</xsl:for-each>
</tr>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
——————————

submenuXSL.xsl
——————————
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="menuname"/>
<xsl:template match="/">
<table border="1">
<xsl:for-each select="//menu[@name=$menuname]|//command[@name=$menuname]">
<xsl:apply-templates select="menu"/>
<xsl:apply-templates select="command"/>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="menu">
<tr>
<td>
<xsl:value-of select="@name"/>&amp;gt;&amp;gt;
</td>
</tr>
</xsl:template>
<xsl:template match="command">
<tr>
<td>
<xsl:value-of select="@name"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
——————————
一个HTTML文件
showxml.htm
——————————
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=gb2312">
<title>无标题文档</title>
<script language="JavaScript" type="text/JavaScript">
<!--
var submenuarray=new Array();
submenuarray[1]=document.all.Layer1;
submenuarray[2]=document.all.Layer2;
submenuarray[3]=document.all.Layer3;
submenuarray[4]=document.all.Layer4;
submenuarray[5]=document.all.Layer5;
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&amp;&amp;(parseInt(appVersion)==4)) {
do
cument.MM_pgW=innerWidth;
document.MM_pgH=innerHeight;
onresize=MM_reloadPage;
}}
else
if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
function MM_findObj(n, d) { //v4.01
var p,i,x;
if(!d) d=document;
if((p=n.indexOf("?"))>0&amp;&amp;parent.frames.length) {
d=parent.frames[n.substring(p+1)].document;
n=n.substring(0,p);}
if(!(x=d[n])&amp;&amp;d.all) x=d.all[n];
for (i=0;!x&amp;&amp;i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&amp;&amp;d.layers&amp;&amp;i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x &amp;&amp;
d.getElementById) x=d.getElementById(n);
return x;
}
function MM_showHideLayers() { //v6.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0;
i<(args.length-2);
i+=3) if ((obj=MM_findObj(args))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style;
v=(v=='show')?'visible':(v=='hide')?'hidden':v;
}
obj.visibility=v;
}
}
function hidesubmenu()
{
for (var i=0;
i<submenuarray.length;i++)
{
MM_showHideLayers('layer'+i,'','hide');
}
}
function showmenu(menuname,layerindex)
{var myxmlDoc=new ActiveXObject("Msxml2.DOMDocument");
myxmlDoc.async=false;
myxmlDoc.load("menu.xml");
var myxslDoc=new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
myxslDoc.async=false;
myxslDoc.load("menuxsl.xsl");
myxslDoc.load("submenu.xsl")
var myxslTemplate=new ActiveXObject("Msxml2.XSLTemplate");
myxslTemplate.stylesheet=myxslDoc;
//alert("菜单名是:"+menuname+" 層偏移是:"+layerindex);
var myxslDocProc = myxslTemplate.createProcessor();
myxslDocProc.input = xmlDoc;
myxslDocProc.addParameter("menuname",menuname,"");
myxslDocProc.transform();
Layer1.innerHTML = myxslDocProc.output;
MM_showHideLayers('Layer'+layerindex,'','show',menuname);
}
//-->
</script>
<SCRIPT language="jscript">
var xmlDoc, xslDoc, xslTemplate;
function init()
{
xmlDoc = new ActiveXObject('MSXML2.DOMDocument.4.0');
xmlDoc.async = false;
xmlDoc.load('menu.xml');
xslDoc=new ActiveXObject('MSXML2.FreeThreadedDOMDocument.4.0');
xslDoc.async = false;
xslDoc.load('menuxsl.xsl');
xslTemplate = new ActiveXObject("Msxml2.XSLTemplate.4.0");
xslTemplate.stylesheet = xslDoc;
var xslDocProc = xslTemplate.createProcessor();
xslDocProc.input = xmlDoc;
xslDocProc.transform();
mainmenu.innerHTML = xslDocProc.output;
}
function show(title)
{
var xslDocProc = xslTemplate.createProcessor();
xslDocProc.input = xmlDoc;
xslDocProc.addParameter("title", title, "");
xslDocProc.transform();
output.innerHTML = xslDocProc.output;
}
</SCRIPT></head>
<body onload="init()">
<div id="bodydiv" style="position:absolute;
width:100%;
height:100%;
z-index:1" onclick="hidesubmenu()"></div>
<div id="mainmenu" style="position:absolute;
width:406px;
height:30px;
z-index:2;
left: 26px;
top: 19px;
background-color: #CCCCCC;
layer-background-color: #CCCCCC;
border: 1px none #000000;"></div>
<div id="Layer1" style="position:absolute;
width:250px;
height:1px;
z-index:3;
left: 26px;
top: 50px;
layer-background-color: #CCCCCC;
border: 1px none #000000;
visibility: hidden;"></div>
<div id="Layer2" style="position:absolute;
width:250px;
height:1px;
z-index:4;
left: 99px;
top: 50px;
background-color: #CCCCCC;
layer-background-color: #CCCCCC;
border: 1px none #000000;
visibility: hidden;">2</div>
<div id="Layer3" style="position:absolute;
width:250px;
height:1px;
z-index:5;
left: 170px;
top: 51px;
background-color: #CCCCCC;
layer-background-color: #CCCCCC;
border: 1px none #000000;
visibility: hidden;"></div>
<div id="Layer4" style="position:absolute;
width:250px;
height:1px;
z-index:6;
left: 256px;
top: 50px;
background-color: #CCCCCC;
layer-background-color: #CCCCCC;
border: 1px none #000000;
visibility: hidden;"></div>
<div id="Layer5" style="position:absolute;
width:250px;
height:1px;
z-index:7;
left: 346px;
top: 50px;
background-color: #CCCCCC;
layer-background-color: #CCCCCC;
border: 1px none #000000;
visibility: hidden;"></div>
</body>
</html>
——————————
原先的想法是在HTML文件刚打开时,将主菜单发表格形式列出,当点击其菜单时,通过调用showmenu(),在参数内传回菜单名(menuname),动态的将相应的菜单内容读出并在<div></div>中显示出来。我在MSXML4的帮助里看到用DOM的addparameter方法,将变量值传递给XSL文件内的变量,但是在我的这个HTML里,却传递不了,如果在submenu.xsl里指定menuname的值,可以正常显示,说明,XPath和XSl文件应该是正确的,只是变量传递不了,或是传递方法不正确。请各位高手帮帮忙啊!我弄了好久了!谢谢
 
这么多天都没人回答啊!
自己搞掂了!
 
后退
顶部