In the XSLT you need to change
<xsl:template match="/">
<!-- How do I use the value of the parameter sent via JavaScript for the cityname (in place of value 'London') -->
<xsl:for-each select="locations/location[cityname='London']">
to
<xsl:param name="cityname"/>
<xsl:template match="/">
<!-- How do I use the value of the parameter sent via JavaScript for the cityname (in place of value 'London') -->
<xsl:for-each select="locations/location[cityname = $cityname]">
I would also set <xsl:output method="html"/>
as you are only creating a fragment of HTML and the XSLT processor does not know that if you don't set the output method.
In your Javascript code for Mozilla, Chrome, Opera I would change the check
else if (document.implementation && document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslt);
/** I believe this is how to set the param, but it didn't work **/
//xsltProcessor.setParameter(null, "cityname", currentLocation);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("WeatherDetailsContainer").appendChild(resultDocument);
}
to
else if (typeof XSLTProcessor !== 'undefined') {
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslt);
xsltProcessor.setParameter(null, "cityname", currentLocation);
var resultFragment = xsltProcessor.transformToFragment(xml, document);
document.getElementById("WeatherDetailsContainer").appendChild(resultFragment);
}
Your IE code with transformNode
does not allow to set parameters, you will need to change that part as well, change
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
ex = xml.transformNode(xslt);
document.getElementById("WeatherDetailsContainer").innerHTML = ex;
}
to
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
var template = new ActiveXObject('Msxml2.XslTemplate');
template.stylesheet = xslt;
var proc = template.createProcessor();
proc.input = xml;
proc.addParameter('cityname', currentLocation);
proc.transform();
document.getElementById("WeatherDetailsContainer").innerHTML = proc.output;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…