好的,正如标题所说,我在使用JavaScript将XML和XSL与我的HTML代码相关联时遇到了问题。我是XML的新手,所以对我来说很容易!这是XML部分:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl" ?>
<CSET2200>
<student>
<lastname>Hardy</lastname>
<firstname>Zachary</firstname>
<id>R09877743</id>
<major>CSET</major>
<age>28</age>
</student>
<student>
<lastname>Boots</lastname>
<firstname>Erin</firstname>
<id>R00334423</id>
<major>SPCH</major>
<age>26</age>
</student>
<student>
<lastname>Thorne</lastname>
<firstname>Lilianna</firstname>
<id>R55668890</id>
<major>ENGL</major>
<age>23</age>
</student>
<student>
<lastname>Hopper</lastname>
<firstname>Chance</firstname>
<id>R66992278</id>
<major>HIST</major>
<age>22</age>
</student>
<student>
<lastname>Laskey</lastname>
<firstname>Chris</firstname>
<id>R43346578</id>
<major>ACCT</major>
<age>21</age>
</student>
<student>
<lastname>Starr</lastname>
<firstname>Ernie</firstname>
<id>R54456609</id>
<major>PHARM</major>
<age>25</age>
</student>
</CSET2200>
这是我用来设置XML文档样式的XSL表单:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Students</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Last Name</th>
<th style="text-align:left">First Name</th>
</tr>
<xsl:for-each select="CSET2200/student">
<tr>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="firstname"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
现在,在这里,它是带有Javascript的HTML部分:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function loadXMLDoc (students.xml)
{
if (window.ActiveXObject)
{
xhttp = new ActiveXObject("Msxm12.XMLHTTP");
}
else
{
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
xhttp.send("");
return xhttp.responseXML;
}
function studentNames()
{
xml = loadXMLDoc("students.xml");
xsl = loadXMLDoc("students.xsl");
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
{
ex = xml.transformNode(xsl);
document.getElementById("example").innerHTML = ex;
}
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("students").appendChild(resultDocument);
}
}
</script>
</head>
<body onload="studentNames()">
<div id="students"></div>
</body>
</html>
我相信后一部分是正确的,我觉得我的问题在于我是如何将XSL与XML链接起来的。任何见解将非常感谢使这个东西正确显示。我并不真的需要在IE中显示它的规定,但它就是为了以防万一。
答案 0 :(得分:0)
也许只是因为 function loadXMLDoc(students.xml) 应该 function loadXMLDoc(filename)