XSL在IE中处理片段 [英] XSL processing a fragment in IE

查看:80
本文介绍了XSL在IE中处理片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Javascript将XML文件的一部分转换为HTML表的单行。我的想法是,我将从多个XML文件中创建表中的多行。对于Firefox和Opera,这个漂亮的小块代码可以很好地工作。

I am trying to use Javascript to transform part of a XML file to a single row of an HTML table. The thought is that I will create multiple rows in the table from multiple XML files. For Firefox and Opera, this nice little chunk of code works beautifully.

var resTable = document.createElement('table');

for (i = 0; i < xmlNames.length; i++)
{
  // code for IE
  if (window.ActiveXObject)
  {
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument)
  {
    xml=loadXMLDoc(xmlNames[i]);
    xsl=loadXMLDoc(xslName);
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    resTable.appendChild(resultDocument);
  }
}

document.getElementById("theDoc").appendChild(resTable);

问题是我在if IE部分尝试了一千件事,而且没有任何东西作品。我做了很多谷歌搜索,并在我问之前在这里浏览,但无济于事。事实上,在SO上有一个未解决的问题,听起来非常相似,但没有回复,没有解决方案,所以我希望有人能够帮助我在这里..

The problem is that I have tried a thousand things in the "if IE" part, and nothing ever works. I've done a lot of googling, and browsing here before I asked, but to no avail. In fact, there is an unanswered question on SO that sounds very similar, but there were no responses and no resolution, so I was hoping someone would be able to help me out here..

我已经成功地获得了整个文档来转换IE,但事实上我想把它作为一个片段来解决我的问题..任何帮助都会非常感激!谢谢!

I've been successful in getting an entire doc to transform on IE, but the fact that I want to do this as a fragment is whats causing my problem.. Any help would be much appreciated! Thanks!

编辑:对不起我忘了提供我的loadXMLDoc功能,以防万一。这是:

Sorry I forgot to provide my loadXMLDoc function in case thats important. Here is is:

function loadXMLDoc(dname)
{
  if (window.XMLHttpRequest)
  {
    xhttp=new XMLHttpRequest();
  }
  else
  {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhttp.open("GET",dname,false);
  xhttp.send("");
  return xhttp.responseXML;
}


推荐答案

经过多次试验和错误我想出了一些有用的东西。这就是我所做的:

After much more trial and error, I came up with something that works. Here's what I did:

像往常一样创建一个xsltProcessor,并调用transform方法。这导致xsltProcessor.output是HTML格式的字符串。当然,我想要一个DOM元素,所以我不得不将HTML字符串转换为DOM。幸运的是,因为我也是XSL样式表的作者,所以我确切地知道我期待什么回来。在我的例子中,输出HTML字符串将是一些< tr> ...< / tr>元素。我最初尝试将resTable(表DOM元素)innerHTML设置为输出字符串,但这不起作用。我仍然不确定为什么,但它似乎有一些与它们是< tr> s有关的事情,并且在表标签的上下文之外设置为innerHTML时无法解析。

Create a xsltProcessor as usual, and call the transform method. This results in xsltProcessor.output being a HTML formatted string. Of course, I want a DOM element, so I had to convert the HTML string to a DOM. Luckily, because I'm the author of the XSL stylesheet too, I know exactly what I'm expecting to come back. In my case, the output HTML string would be some number of <tr>...</tr> elements. I initially tried setting my resTable (a table DOM element) innerHTML to the output string, but that did not work. I'm still not sure why, but it seems like it has something specific to do with the fact they were <tr>s and it wasn't able to be parsed when set to innerHTML outside the context of a table tag.

无论如何,我创建了一个临时div元素,并将ITs innerHTML设置为一个字符串,其中xsltProcessor的输出字符串包含在< table>< / table>标记中。现在temp div元素是DOM表,然后我逐步完成并抓住子节点(这是xsl处理器首先返回的tr节点)。这样做有点荒谬,但它确实有效,这是我第一次说出来......这是我测试的所有浏览器中的最终版本..

At any rate, I created a temporary div element and set ITs innerHTML to a string having the xsltProcessor's output string encased in a <table></table> tag. Now the temp div element is DOM table, which I then stepped through and grabbed just the child nodes (which are the tr nodes that the xsl processor returned in the first place). Seems kind of ridiculous to do all this, but it works, and thats the first time I can say that.. Here's the final version that works in all the browsers I've tested..

var resTable = document.createElement('table');

for (i = 0; i < xmlNames.length; i++)
{
  // code for IE
  if (window.ActiveXObject)
  {
    var xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.load(xmlNames[i]);

    var xslt = new ActiveXObject("Msxml2.XSLTemplate");
    var xsl = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.3.0");

    var xsltProcessor;
    xsl.async = false;
    xsl.resolveExternals = false;
    xsl.load(xslName);
    xslt.stylesheet = xsl;

    xsltProcessor = xslt.createProcessor();
    xsltProcessor.input = xml;

    //This transform results in one or more tr.../tr HTML tag(s)
    xsltProcessor.transform();

    //Create a temp div element which is used to convert the HTML
    //string to a DOM element so I can grab just the part I want..
    tmp = document.createElement('div');

    //Can't set innerHTML to tr tags directly I guess, so have to put
    //in context of a table so it can be parsed...
    tmp.innerHTML = "<table>" + xsltProcessor.output + "</table>";

    //Now I need to grad the tr children from inside the table node, since
    //the table was only to please the parser
    for (tmpChildInd = 0; tmpChildInd <  tmp.childNodes[0].childNodes.length;   tmpChildInd++)
    {
      //finally, append the temporary elements children (the tr tags) 
      //to the overall table I created before the loop.
      resTable.appendChild(tmp.childNodes[0].childNodes[tmpChildInd]);
    }  
  }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation && document.implementation.createDocument)
  {
    xml=loadXMLDoc(xmlNames[i]);
    xsl=loadXMLDoc(xslName);
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    resTable.appendChild(resultDocument);
  }
}

//put the full table at the div location "theDoc" now..
document.getElementById("theDoc").appendChild(resTable);

不确定人们多久会尝试这样做,但希望这有助于其他人...

Not sure how often folks try to do this, but hopefully this helps someone else out there..

这篇关于XSL在IE中处理片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆