非常简单的代码,我无法使其工作。 XSLT中的参数始终为空。我错过了什么?我正在使用FF6。请你帮忙,你们的眼睛很敏锐。谢谢!
的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 :(得分:3)
setParameter()将设置一个全局变量(参数)。
您需要将param-element移出template-element,使其成为stylesheet-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>