admin管理员组文章数量:1389871
I have scoured through numerous posts that try and explain how to achieve this, however none have helped me solve the problem.
I have a HTML file which contains an XML parsing JavaScript function which in turn renders the XSLT file. The issue is, I have multiple 'records' in my XML file and I only want a single XSLT to render each of records (instead of a separate XSLT file per record). Based on the code below, please could someone advise on how I can pass a parameter from JavaScript containing the record id (in each XML record in my XML file) so that the 'single' XSLT can parse the layout with the correct data sourced from the parameter.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" />
<title>Weather App</title>
<link href="../../css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection" />
<link href="../../css/animate.css" type="text/css" rel="stylesheet" />
<link href="../../css/style.css" type="text/css" rel="stylesheet" media="screen,projection" />
</head>
<body>
<div id="WeatherDetailsContainer"></div>
<!-- Scripts-->
<script src=".1.1.min.js"></script>
<script src="../../js/materialize.js"></script>
<script>
$(function(){
RenderXSLT();
});
function loadXMLDoc(filename) {
if (window.ActiveXObject) {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
try {
xhttp.responseType = "msxml-document"
} catch (err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}
function RenderXSLT() {
xml = loadXMLDoc("datastore/Weather.xml");
xslt = loadXMLDoc("transformations/WeatherDetailsCard.xslt");
var currentLocation = localStorage.getItem('current_weather_location');
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
ex = xml.transformNode(xslt);
document.getElementById("WeatherDetailsContainer").innerHTML = ex;
}
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);
}
}
</script>
</body>
</html>
XML File
<?xml version="1.0" encoding="UTF-8" ?>
<locations>
<location>
<cityid>Lon</cityid>
<cityname>London</cityname>
<temperature>11</temperature>
<high>13</high>
<low>4</low>
<date>17/03/2015</date>
</location>
<location>
<cityid>Man</cityid>
<cityname>Manchester</cityname>
<temperature>07</temperature>
<high>08</high>
<low>2</low>
<date>17/03/2015</date>
</location>
<location>
<cityid>Gla</cityid>
<cityname>Glasgow</cityname>
<temperature>05</temperature>
<high>06</high>
<low>1</low>
<date>17/03/2015</date>
</location>
</locations>
XSLT File
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
<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']">
<div class="section">
<div class="container">
<div class="row">
<div class="col s4 m4 l4">
<div class="card-panel z-depth-3 animated fadeInUp" style="padding:10px 10px 5px 10px !important;">
<span class="center-align">
<h5><xsl:value-of select="cityname"/></h5><span> (<xsl:value-of select="cityid"/>)</span>
</span>
<p>Temp: <xsl:value-of select="temperature"/></p>
<p>High: <xsl:value-of select="high"/></p>
<p>Low: <xsl:value-of select="low"/></p>
</div>
</div>
</div>
</div>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have scoured through numerous posts that try and explain how to achieve this, however none have helped me solve the problem.
I have a HTML file which contains an XML parsing JavaScript function which in turn renders the XSLT file. The issue is, I have multiple 'records' in my XML file and I only want a single XSLT to render each of records (instead of a separate XSLT file per record). Based on the code below, please could someone advise on how I can pass a parameter from JavaScript containing the record id (in each XML record in my XML file) so that the 'single' XSLT can parse the layout with the correct data sourced from the parameter.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no" />
<title>Weather App</title>
<link href="../../css/materialize.css" type="text/css" rel="stylesheet" media="screen,projection" />
<link href="../../css/animate.css" type="text/css" rel="stylesheet" />
<link href="../../css/style.css" type="text/css" rel="stylesheet" media="screen,projection" />
</head>
<body>
<div id="WeatherDetailsContainer"></div>
<!-- Scripts-->
<script src="https://code.jquery./jquery-2.1.1.min.js"></script>
<script src="../../js/materialize.js"></script>
<script>
$(function(){
RenderXSLT();
});
function loadXMLDoc(filename) {
if (window.ActiveXObject) {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
try {
xhttp.responseType = "msxml-document"
} catch (err) {} // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}
function RenderXSLT() {
xml = loadXMLDoc("datastore/Weather.xml");
xslt = loadXMLDoc("transformations/WeatherDetailsCard.xslt");
var currentLocation = localStorage.getItem('current_weather_location');
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
ex = xml.transformNode(xslt);
document.getElementById("WeatherDetailsContainer").innerHTML = ex;
}
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);
}
}
</script>
</body>
</html>
XML File
<?xml version="1.0" encoding="UTF-8" ?>
<locations>
<location>
<cityid>Lon</cityid>
<cityname>London</cityname>
<temperature>11</temperature>
<high>13</high>
<low>4</low>
<date>17/03/2015</date>
</location>
<location>
<cityid>Man</cityid>
<cityname>Manchester</cityname>
<temperature>07</temperature>
<high>08</high>
<low>2</low>
<date>17/03/2015</date>
</location>
<location>
<cityid>Gla</cityid>
<cityname>Glasgow</cityname>
<temperature>05</temperature>
<high>06</high>
<low>1</low>
<date>17/03/2015</date>
</location>
</locations>
XSLT File
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3/1999/XSL/Transform">
<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']">
<div class="section">
<div class="container">
<div class="row">
<div class="col s4 m4 l4">
<div class="card-panel z-depth-3 animated fadeInUp" style="padding:10px 10px 5px 10px !important;">
<span class="center-align">
<h5><xsl:value-of select="cityname"/></h5><span> (<xsl:value-of select="cityid"/>)</span>
</span>
<p>Temp: <xsl:value-of select="temperature"/></p>
<p>High: <xsl:value-of select="high"/></p>
<p>Low: <xsl:value-of select="low"/></p>
</div>
</div>
</div>
</div>
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Share
edited Aug 20, 2016 at 9:45
vitr
7,1548 gold badges32 silver badges52 bronze badges
asked Mar 18, 2015 at 14:16
user1163073user1163073
3177 silver badges20 bronze badges
2 Answers
Reset to default 5In 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;
}
It looks like you had the right code in your Javascript for passing in the parameter, but your XSLT has to also be written to accept the parameter:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3/1999/XSL/Transform">
<!-- Declare it here -->
<xsl:param name="cityName" />
<xsl:template match="/">
<!-- use it here ------v -->
<xsl:for-each select="locations/location[cityname = $cityName]">
...
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
本文标签: htmlHow to pass parameter to XSLT from Javascript functionStack Overflow
版权声明:本文标题:html - How to pass parameter to XSLT from Javascript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652055a2617743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论