Let's say I have the following XML
file..
<?xml version="1.0" encoding="UTF-8"?>
<products>
<test>
<id>test_value1</id>
<value>1</value>
</test>
<test>
<id>test_value2</id>
<value>2</value>
</test>
</products>
And the following XSLT file..
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<xsl:for-each select="products/test">
<xsl:if test="id='test_value1'">
<a href="#"> Hello #{value} </a>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And I'm loading this data into a HTML page using the following code..
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(filename) {
if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.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 displayXMLDoc(xml_file, xsl_file, element_id) {
xml = loadXMLDoc(xml_file);
xsl = loadXMLDoc(xsl_file);
// BROWSER IS IE / EDGE.
if (window.ActiveXObject) {
ex = xml.transformNode(xsl);
document.getElementById(element_id).innerHTML = ex;
}
// BROWSER IS MOZILLA, FIREFOX, OPERA, ETC.
else if (document.implementation && document.implementation.createDocument) {
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
var resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById(element_id).appendChild(resultDocument);
}
}
function toDisplay(){
displayXMLDoc('test.xml', 'test.xsl', 'test_output');
}
</script>
</head>
<body onload="toDisplay()">
<div id="test_output"></div>
</body>
</html>
How can I pass a value from HTML into the XSLT file so that the test_value1
in <xsl:if test='id='test_value1'">
is equal to the value passed from HTML, using JavaScript?
For example, if I wanted to load the second tag values using the same code found here, how would I do it?
My current thought process would be that I could assign the id as an
id='' tag
in the XSLT, but I'm unsure of how to load the data from HTML.