2011-08-22 56 views
1

非常简单的代码,我只是无法让它工作。 XSLT中的参数始终为空。我错过了什么?我正在使用FF6。请帮助,你的家伙尖锐的眼睛。谢谢!
XSLT没有从javascript传递参数

的index.html

<html> 
<head> 
<script> 
function loadXMLDoc(dname) { 
    xhttp = new XMLHttpRequest(); 
    xhttp.open("GET", dname, false); 
    xhttp.send(""); 
    return xhttp.responseXML; 
} 
function displayResult(source,styledoc,section) { 
    xml = loadXMLDoc(source); 
    xsl = loadXMLDoc(styledoc); 

    if (window.ActiveXObject) { 
     ex = xml.transformNode(xsl); 
     document.getElementById("display").innerHTML = ex; 
    } 
    else if (document.implementation && document.implementation.createDocument) { 
     xsltProcessor = new XSLTProcessor(); 
     xsltProcessor.importStylesheet(xsl); 
     xsltProcessor.setParameter(null,"section",section); 
     alert(xsltProcessor.getParameter(null,"section")); 
     resultDocument = xsltProcessor.transformToFragment(xml, document); 
     document.getElementById("display").appendChild(resultDocument); 
    } 
} 
</script> 
</head> 
<body onload="displayResult('test.xml','test.xslt','somevalue')"> 
<div id="display"/> 
</body> 
</html> 

test.xslt

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <xsl:param name="section"/> 
     section=<xsl:value-of select="$section"/> 
    </xsl:template> 
</xsl:stylesheet> 

的test.xml

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="test.xslt"?> 
<test/> 
+0

和你在脚本的firdt部分传递参数的地方? (IE版) – SergeS

回答

3

的setParameter()将设置一个全局变量(参数)。

您需要将参数元素移出template-element以使其成为样式表元素的子元素,否则将覆盖全局参数。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:param name="section"/> 
<xsl:template match="/"> 
     section=<xsl:value-of select="$section"/> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感谢!这个看起来如此简单的问题让我一整天都在逃避! –

+0

如果您想知道如何在IE中设置变量,请参阅http://msdn.microsoft.com/en-us/library/ms762312%28v=VS.85%29.aspx;) –