gpt4 book ai didi

javascript - 无法将下拉值传递给 XSLT 参数

转载 作者:行者123 更新时间:2023-12-02 21:13:13 26 4
gpt4 key购买 nike

我试图将下拉列表中的选项传递到 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 时这不起作用。任何见解都会有很大帮助。

最佳答案

您需要使用 XSLT 将 XSLT 文档作为 XML DOM 文档或其本身进行操作,以更改 select或您当前想要的其他 XPath 表达式。

如果您将参数传递给 XSLT,那么它们的类型为字符串、数字或 bool 值,并且在您使用字符串、数字或 bool 值的 XPath 中使用,因此您可以使用例如

 <select id="schoolSelect">
<option value="">ALL</option>
<option value="Adult-Education">Adult-Education</option>
<option value="Pre-School">Pre-School</option>
</select>


<xsl:param name="schoolTypes" select="''"/>

 <xsl:variable name="schools" select="*[local-name() = $schoolTypes or $schoolTypes = '']/School[contains(Name,$schoolName)]"/>

然后Number of matches: <xsl:value-of select="count($schools)"/>for-eachselect="$schools" .

关于javascript - 无法将下拉值传递给 XSLT 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61037685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com