Something’s screwy here. I’m trying to parse this XSL file:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Events</title>
</head>
<body>
<dl>
<xsl:for-each select="all/event">
<xsl:sort select="substring(@displayuntil,7,4)" data-type="number" /> <!-- year -->
<xsl:sort select="substring(@displayuntil,1,2)" data-type="number" /> <!-- month -->
<xsl:sort select="substring(@displayuntil,4,2)" data-type="number" /> <!-- day -->
<dt>
<xsl:value-of select="@displayuntil" />
--
<xsl:value-of select="@title" />
</dt>
<dd>
<xsl:value-of select="description" disable-output-escaping="yes" />
</dd>
</xsl:for-each>
</dl>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Using the following JavaScript code, taken from w3schools:
function loadXMLDoc(dname) {
if (window.XMLHttpRequest) {
var xhttp = new XMLHttpRequest();
} else {
var xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
alert(xhttp.responseText); // displays the contents of the document
alert(xhttp.responseXML.xml); // in IE, displays nothing
return xhttp.responseXML;
}
The xhttp.responseText alert displays my document, so I know it’s loading. The xhttp.responseXML.xml alert is blank, so apparently it isn’t a well-formed XML file.
I’ve run it through http://XMLvalidation.com and get no errors. So what have I overlooked?
It works for me.
I just attached that javascript code to a button click, and put the xsl into a file on the server. When I clicked the button, I got two alerts, both with basically the same content.
It may be that you are running into a stale cache. I ran into this just yesterday. I was using XHR to retrieve an XML file, and i was getting an old copy. If you’ve been changing the xsl, it could be the doc the web page retrieves is an old version, which could also be invalid. To avoid that , append
?_<random number>to the URL.Also you should declare your var at most once in a function. In Javascript, vars have function scope, so regardless where you put a
vardeclaration, that variable name is known throughout the function. Which means it is good coding style to put all vars at the top of the function, regardless where you use them. In fact it is a conflict to declare a var with the same name in the if clause as well as the else clause; I don’t know what the JS engine will do about that, but it is strictly speaking not sensible. Easy to change though.EDIT – Lastly, you should use the updated ProgId. See this blog post from Microsoft for an explanation. Actually you don’t need the explanation, the short story is,
Microsoft.XMLHTTPis wrong. What you should use isMSXML2.XMLHTTP.With those changes my code looks like this:
If that no-cache trick doesn’t work then you need to diagnose further. If I were trying to diagnose this, I would simplify the XSL file – make it just an XML file, very simple, and see if you can get that to work. Then gradually add back complexity. That will allow you to avoid or rule out the invalid/malformed XML problem.