Я пытаюсь передать выбор из раскрывающегося списка в мой XSLT, чтобы отфильтровать результаты. Хотя по какой-то причине он не хочет работать должным образом.
Когда я жестко кодирую соответствующее значение в запросе, это дает желаемый результат. Попытка погуглить эту проблему в любом случае также не принесла особой помощи. Я полагаю, что проблема может заключаться в том, что xsltProcessor.setParameter анализирует данные, неправильно отправляя их в XSLT.
XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Source: https://en.wikipedia.org/wiki/List_of_schools_in_London,_Ontario [March 12, 2020] -->
<London-Schools>
<Adult-Education>
<School short-name="G.A. Wheable" french-immersion="no" before-after-programs="no">
<Board>Thames Valley District School Board</Board>
<Address>70 Jacqueline St</Address>
<Name>G.A. Wheable Adult Education Centre</Name>
<Coordinates x="482066.845" y="4757710.908" />
<Website>https://www.tvdsb.ca/en/parents/adult-and-continuing-education.aspx</Website>
</School>
<School short-name="Centre for Lifelong Learning" french-immersion="no" before-after-programs="no">
<Board>London District Catholic School Board</Board>
<Address>1230 King St</Address>
<Name>Centre for Lifelong Learning St. Patrick Campus</Name>
<Coordinates x="483095.5923" y="4760265.75" />
<Website>http://cfll.ldcsb.ca/</Website>
</School>
</Adult-Education>
<Pre-School>
<School short-name="Gibbons Park Montessori" french-immersion="no" before-after-programs="no">
<Board>Private</Board>
<Address>29 Victoria St</Address>
<Name>Gibbons Park Montessori Private School</Name>
<Coordinates x="478120.7488" y="4760883.317" />
<Website>http://gibbonsparkmontessori.com/</Website>
</School>
</Pre-School>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="schoolTypes" select="//*"/>
<xsl:param name="schoolName" select="//*"/>
<xsl:template match="London-Schools">
<html>
<body>
<h2>My Schools</h2>
Number of matches: <xsl:value-of select="count($schoolTypes/School[Name=contains(text(),$schoolName)])"/>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">School</th>
<th style="text-align:left">Board</th>
<th style="text-align:left">Address</th>
</tr>
<xsl:for-each select="$schoolTypes/School">
<xsl:sort select="@short-name"/>
<tr>
<!--<td><xsl:value-of select="Name"/></td>-->
<td>
<a href="{Website}">
<xsl:value-of select="Name"/>
</a>
</td>
<td>
<xsl:value-of select="Board"/>
</td>
<td>
<xsl:value-of select="Address"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Javascript:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
h1 {
font-family: Verdana;
font-size: 24pt;
}
h2 {
font-family: Verdana;
font-size: 18pt;
}
input, button {
font-family: Verdana;
font-size: 14pt;
}
</style>
<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>Schools in Area</title>
</head>
<body>
<select id="schoolSelect">
<option value="//*">ALL</option>
<option value="//Adult-Education">Adult-Education</option>
<option value="//Pre-School">Pre-School</option>
</select>
<input type="text" id="schoolInput" />
<input type="submit" value="Submit" onclick="RenderXSLT()" />
<div id="xsltOutputContainer"></div>
<script type="text/javascript">
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) {}
xhttp.send("");
return xhttp.responseXML;
}
function RenderXSLT()
{
xml = loadXMLDoc("schools.xml");
xslt = loadXMLDoc("Schools.xslt");
var characterName = document.getElementById("schoolInput").value;
characterName = characterName.toUpperCase();
var schoolType = document.getElementById("schoolSelect").value;
schoolType = schoolType.toUpperCase();
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
{
var template = new ActiveXObject("Msxml2.XslTemplate.6.0");
template.stylesheet = xslt;
var proc = template.createProcessor();
proc.input = xml;
proc.addParameter("schoolName", characterName);
proc.addParameter("schoolChoice", schoolType);
proc.transform();
document.getElementById("xsltOutputContainer").innerHTML = proc.output;
}
else if (typeof XSLTProcessor !== 'undefined')
{
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xslt);
xsltProcessor.setParameter(null, "schoolName", characterName);
xsltProcessor.setParameter(null, "schoolTypes", schoolSelect);
var resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("xsltOutputContainer").innerHTML = "";
document.getElementById("xsltOutputContainer").appendChild(resultDocument);
}
}
</script>
</body>
</html>
Я совершенно озадачен тем, почему это не сработает при попытке пройти через Javascript. Любые идеи будут очень полезны.