我想向 html 页面添加一个表格 其数据来自 AJAX 上的 xml 服务器。我想通过 XML 转换来做到这一点。在服务器端,转换工作并根据需要显示表格,但在客户端,我总是遇到整个 XML 文档,而不是转换后的表格。实际上,下面的 XMLHttpRequest.responseXML 包含完整的 XML 文档。如果有任何回复,我将不胜感激。
JavaScript代码摘录(接收ajax响应的回调函数):
[...]
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
output = xmlhttp.responseXML;
document.getElementById("result").innerHTML = output;
}
[...]
在服务器端,只是为了测试的目的,我设置了一个包含以下内容的 JSP 页面(如果我启动显示所需表格的 JSP 页面):
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<?xml-stylesheet type="text/xsl" href="generateXMLXSL.xsl" ?>
<bookstore>
<book publisher="Kiskapu" year="2004">
<author>John Doe</author>
<title>C++ Programozas</title>
<review>Nem tul jo.</review>
</book>
<book publisher="Manning" year="2009">
<author>D. Malavia</author>
<title>SCWD Exam Study Kit</title>
<review>Excellent reading for the exam.</review>
</book>
<book publisher="Manning" year="2013">
<author>Andrew Doe</author>
<title>Web Application Development</title>
<review>Medium</review>
</book>
<book publisher="Sage" year="2008">
<author>Salkind</author>
<title>Exploring Research</title>
<review>Excellent, easy-to-read and interesting!</review>
</book>
</bookstore>
在服务器端工作的简单 xsl 文件:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<table cellspacing="1">
<tr style="background-color: #cccccc">
<th>Author</th>
<th>Title</th>
<th>Review</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr style="background-color: #dedede">
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="review"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
解决方案: Martin 提请我注意这样一个事实:在客户端,如果 xml 通过 XMLHttpRequest 对象传入,则 xsl 文件不会被处理。这回答了我遇到的错误。如何根据变量从 javascript 转换 xml 的解决方案可以在这里找到:
最佳答案
解决方案:Martin 提请我注意这样一个事实:在客户端,如果 xml 通过 XMLHttpRequest 对象传入,则不会处理 xsl 文件。这回答了我遇到的错误。如何根据变量从 javascript 转换 xml 的解决方案可以在这里找到:
关于javascript - HTML 页面中的 XML/XSL 转换表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24839681/