Skip to content

Instantly share code, notes, and snippets.

@artemkonenko
Created March 6, 2017 11:41
Show Gist options
  • Save artemkonenko/0ef21a7d4cc66d89f127b0705a3b1b77 to your computer and use it in GitHub Desktop.
Save artemkonenko/0ef21a7d4cc66d89f127b0705a3b1b77 to your computer and use it in GitHub Desktop.
Routine for getting xml file, transforming it by xsl file and putting the result to select
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 buildXML(xmlPath, xslPath)
{
const xml = loadXMLDoc(xmlPath);
const xsl = loadXMLDoc(xslPath);
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
{
ex = xml.transformNode(xsl);
return ex;
}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
return resultDocument;
}
}
$(document).ready(function(){
$('.selectpicker').selectpicker({
style: 'btn-info'
// size: 4
});
const list = buildXML('/xslt/list.xml', '/xslt/discipline_selector_options.xsl');
console.log(list);
$('.selectpicker').html(list);
$('.selectpicker').selectpicker('refresh');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment