2011-08-22 58 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;) –