JavaScript的XSLTProcessor中偶尔会无法正常工作(javascript XSLT

2019-07-28 21:21发布

下面的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代码,有时它没有。 任何人都知道其中的问题呢?

Answer 1:

你是被迫进入同步的解决方案使用的是现在,或者是一个异步解决方案的选择呢? 我记得火狐已是与过去同步调用共享的问题,我不知道有多少的这仍然是与它进行。 我曾经见过整个Firefox的界面将只要锁定为作为请求正在运行(这取决于超时设置,可能需要很长的时间)的情况。

这将需要您投入更多的工作,但解决办法是像下面这样。 这是我用XSLT处理用的东西的Ajax代码(改写稍微因为我的代码是面向对象的,并包含一个循环,从第一次加载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();
    }
  }
}

和ofcourse你需要产生第二组函数来处理XSL加载(从开始就loadXSLDoc,例如)。

然后,在你结束processXSLReqChange你可以抓住你的XML结果和XSL结果,并做了改造。



Answer 2:

好了,下面的代码对IE完全不同的路径,一切-ELSE。 我认为问题仅限于其中之一。 有什么浏览器,你试了一下,并表现出这个错误?

我能想到的唯一的另一件事是,当你试图做的东西给它的popularTags元素可能不存在。 这是如何的功能正在执行? 在一个onload / domready中的事件吗?



Answer 3:

担。 IE执行没有问题的脚本。 我对着在Firefox的问题。 该popularTags元素不存在,调用该函数的HTML文件内。

<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>    



Answer 4:

为了避免事情并行加载(由旦暗示)的问题,它始终是一个好主意,打电话只有当页面完全加载此类脚本。

理想情况下,你把脚本标签在页面头部和调用ShowPopularTags(); 在身体的Onload项目。 即

<BODY onLoad="ShowPopularTags();">

你完全确定你的document.getElementById(“popularTags”)并没有失败,因为脚本调用之前包含该元素的HTML完全加载的方式。

另外,我们可以看到你的XMLDocLoad功能? 如果包含非连续的元素,以及,你可能会面临在XSLT转换发生之前的对象XML和XSL是满载的一个问题。



Answer 5:

以下是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');
    }


}


文章来源: javascript XSLTProcessor occasionally not working