2011-06-20 71 views
0

我正在嘗試編寫我的第一個Firefox擴展。該擴展應該使用XSLT以很好的方式顯示FOAF文件。現在我只想在按下按鈕時將XSL樣式表添加到rdf文件中。該函數被調用,但rdf文件的顯示不會改變。javascript:動態添加xsl樣式表到XML數據的問題

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

function displayMyResult() 
{ 
    alert("test") 
    xml=loadXMLDoc("http://www.example.com/me.rdf"); 
    xsl=loadXMLDoc("http://www.example.com/test.xsl"); 
    if (window.ActiveXObject) 
    { 
      ex=xml.transformNode(xsl); 
      content.document.location.replace(ex) 
    } 
    // code for Mozilla, Firefox, Opera, etc. 
    else if (document.implementation && document.implementation.createDocument) 
    { 
      xsltProcessor=new XSLTProcessor(); 
      xsltProcessor.importStylesheet(xsl); 
      resultDocument = xsltProcessor.transformToFragment(xml,document); 
      content.document.location.replace(ex) 
    } 
} 

第一個函數loadXMLDoc從另一張貼在這裏複製的,而且應該儘量努力。 Probem在displayMyResult方法中。測試警報確認,該函數被調用,但me.rdf文件沒有顯示任何不同。

我認爲行content.document.location.replace(ex)是錯誤的,但沒有在網上找到任何東西,將解釋給我什麼來代替。

任何人都可以告訴我如何加載XLST樣式表來呈現RDF文件嗎?

回答

0

爲什麼你的代碼打算用於Mozilla擴展程序檢查IE對象,如「ActiveXObject」? 無論如何,你的代碼沒有多大意義,你的Mozilla分支從不分配給名爲ex的變量,然後你可以調用replace(ex)。 一些更有意義的代碼將是

var resultFragment = xsltProcessor.transformToFragment(xml, content.document); 
content.document.replaceChild(resultFragment, content.document.documentElement); 

但我不知道這會在普通的工作,特別是如果content.document是不同類型比XSLT的結果文件(即一個爲HTML文檔另一個是SVG文檔)。