javascript XSLTProcessor有时不起作用
以下JavaScript假定从XML文件中读取流行标签,并应用XSL样式表并将其作为HTML输出到浏览器。
function ShowPopularTags() {
xml = XMLDocLoad("http://localhost/xml/tags/popular.xml?s=94987898");
xsl = XMLDocLoad("http://localhost/xml/xsl/popular-tags.xsl");
if (window.ActiveXObject) {
// code for IE
ex = xml.transformNode(xsl);
ex = ex.replace(/\/g, "");
document.getElementById("popularTags").innerHTML = ex;
} else if (document.implementation && document.implementation.createDocument) {
// code for Mozilla, Firefox, Opera, etc.
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("popularTags").appendChild(resultDocument);
var ihtml = document.getElementById("popularTags").innerHTML;
ihtml = ihtml.replace(/\/g, "");
document.getElementById("popularTags").innerHTML = ihtml;
}
}
ShowPopularTags();
该脚本的问题是,有时它设法输出结果HTML代码,有时则不输出。有人知道哪里出了问题吗?
解决方案
好的,对于IE和其他所有代码,它们遵循完全不同的路径。我认为问题仅限于其中之一。我们尝试过哪种浏览器,哪些浏览器出现此错误?
我唯一想到的是,当我们尝试对其进行填充时,popularTags元素可能不存在。该函数如何执行?在上载/准备就绪事件中?
担。 IE可以毫无问题地执行脚本。我在Firefox中面临问题。 HTML文件中存在PopularTags元素,该元素调用该函数。
<div id="popularTags" style="line-height:18px"></div>
<script language="javascript" type="text/javascript">
function ShowPopularTags()
{
xml=XMLDocLoad("http://localhost/xml/tags/popular.xml?s=29497105");
xsl=XMLDocLoad("http://localhost/xml/xsl/popular-tags.xsl");
if (window.ActiveXObject){
// code for IE
ex=xml.transformNode(xsl);
ex = ex.replace(/\/g, "");
document.getElementById("popularTags").innerHTML=ex;
}
else if (document.implementation && document.implementation.createDocument){
// code for Mozilla, Firefox, Opera, etc.
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("popularTags").appendChild(resultDocument);
var ihtml = document.getElementById("popularTags").innerHTML;
ihtml = ihtml.replace(/\/g, "");
document.getElementById("popularTags").innerHTML = ihtml;
}
}
ShowPopularTags();
</script>
为了避免并行加载的问题(如Dan所暗示的),仅在页面完全加载后才调用此类脚本总是一个好主意。
理想情况下,我们将脚本标签放在页面标题中,然后调用ShowPopularTags();。在上载项目中。 IE。
<BODY onLoad="ShowPopularTags();">
这样,我们完全可以确保document.getElementById(" popularTags")不会失败,因为在完全加载包含元素的HTML之前调用了脚本。
另外,我们可以看到XMLDocLoad函数吗?如果其中也包含非顺序元素,则可能会遇到一个问题,即XSLT转换发生在对象xml和xsl完全加载之前。
以下是XMLDocLoad函数。
function XMLDocLoad(fname)
{
var xmlDoc;
if (window.ActiveXObject){
// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}
else if(document.implementation && document.implementation.createDocument){
// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}
else{
alert('Your browser cannot handle this script');
}
}
我们是否被迫进入现在正在使用的同步解决方案,还是也可以选择异步解决方案?我记得Firefox在过去曾遇到过同步调用方面的问题,但我不知道它还会带来多少问题。我看到了这样的情况,整个Firefox界面将在请求运行期间一直锁定(根据超时设置,这可能需要很长时间)。
这将需要我们做更多的工作,但是解决方案将类似于以下内容。这是我用Ajax处理XSLT东西的代码(略微改写是因为我的代码是面向对象的,并且包含一个循环,该循环从首次加载的XML文档中解析出相应的XSL文档)
注意:请确保在函数之外声明oCurrentRequest和oXMLRequest的版本,因为它将被继承。
if (window.XMLHttpRequest)
{
oCurrentRequest = new XMLHttpRequest();
oCurrentRequest.onreadystatechange = processReqChange;
oCurrentRequest.open('GET', sURL, true);
oCurrentRequest.send(null);
}
else if (window.ActiveXObject)
{
oCurrentRequest = new ActiveXObject('Microsoft.XMLHTTP');
if (oCurrentRequest)
{
oCurrentRequest.onreadystatechange = processReqChange;
oCurrentRequest.open('GET', sURL, true);
oCurrentRequest.send();
}
}
之后,我们只需要一个名为processReqChange的函数即可,该函数包含以下内容:
function processReqChange()
{
if (oCurrentRequest.readyState == 4)
{
if (oCurrentRequest.status == 200)
{
oXMLRequest = oCurrentRequest;
oCurrentRequest = null;
loadXSLDoc();
}
}
}
当然,我们还需要产生第二组函数来处理XSL加载(例如,从loadXSLDoc开始)。
然后,在processXSLReqChange的最后,我们可以获取XML结果和XSL结果并进行转换。

