2011-09-08 77 views
0

我正在嘗試使用Javascript將XML文件的一部分轉換爲HTML表格的單個行。我的想法是,我將在多個XML文件的表中創建多行。對於Firefox和Opera來說,這一小部分代碼可以很好地工作。XSL在IE中處理片段

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」部分嘗試了一千件東西,而且沒有任何工作。我做了很多Google搜索,在我問之前在這裏瀏覽,但無濟於事。事實上,在SO上有一個沒有答案的問題,聽起來很相似,但沒有迴應,也沒有解決方案,所以我希望有人能夠幫助我在這裏..

我已經成功地獲得整個文檔在IE上進行轉換,但是我想將其作爲一個片段進行轉換,這是造成我的問題的原因。任何幫助都將非常感謝!謝謝!

編輯:對不起,我忘記提供我的loadXMLDoc函數的情況下,重要的。這裏是:

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; 
} 
+0

對於你不應該嘗試使用window.ActiveXObject來檢測IE,即使啓用了ActiveX過濾,它也會評估爲true,所以如果你以後嘗試創建一個ActiveX對象,它將會失敗。 – Joey

+0

好的,那麼有沒有建議,然後以另一種方式呢? – daroo

+0

http://blogs.msdn.com/b/ie/archive/2011/05/02/activex-filtering-for-developers.aspx - 你已經爲XHR做了正確的事情。 – Joey

回答

2

經過更多的試驗和錯誤,我想出了一些工作。以下是我所做的:

像往常一樣創建xsltProcessor,並調用transform方法。這導致xsltProcessor.output是HTML格式的字符串。當然,我想要一個DOM元素,所以我必須將HTML字符串轉換爲DOM。幸運的是,因爲我也是XSL樣式表的作者,所以我確切地知道我期望回來什麼。在我的情況下,輸出HTML字符串將會是一些< tr> ... </tr>元素。我最初嘗試將resTable(一個表DOM元素)innerHTML設置爲輸出字符串,但那不起作用。我仍然不確定爲什麼,但它似乎有一些特定的事實,它們是< tr> s,並且在設置爲tableHTML上下文之外的innerHTML時無法解析。

無論如何,我創建了一個臨時div元素,並將IT innerHTML設置爲一個字符串,該字符串的xsltProcessor輸出字符串包含在<表> </table>標記中。現在temp div元素是DOM表,然後我通過它來抓取子節點(它們是xsl處理器首先返回的tr節點)。似乎有點可笑做這一切,但它的作品,並且那是我第一次可以說...這裏的是,在所有我測試過的瀏覽器上運行的最終版本..

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

不知道如何經常人們試圖做到這一點,但希望這可以幫助其他人..