I would like to add a table to a html page the data of which come from a server in xml over AJAX. I wanted to do it with XML transformation. On the server side the transformation works and displays the table as wished but on the client side I always encounter the whole XML document not the transformed table. Actually, the below XMLHttpRequest.responseXML contains the full XML document. I would appreciate any response.
Excerpt form the javascript code (the callback function that receives the ajax response):
[...]
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
output = xmlhttp.responseXML;
document.getElementById("result").innerHTML = output;
}
[...]
On the server side just for the test's sake, I set up a JSP page with the following content (if I start up the JSP page that displays the table as wished):
<%@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>
The simple xsl file that works on the server side:
<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>
Solution: Martin drew my attention to the fact that on the client side if the xml comes via the XMLHttpRequest object, then the xsl file is not processed. This answers the error I encountered. The solution how to transform the xml from javascript based on variables can be found here: