繁体   English   中英

XSLT没有从JavaScript传递参数

[英]XSLT not getting parameter passed from javascript

非常简单的代码,我只是无法使其工作。 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/>

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

您需要将param-element移出template-element,使其成为stylesheet-element的子代,否则它将覆盖global参数。

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM