2011-09-08 2 views
0

XML 파일의 일부를 HTML 테이블의 단일 행으로 변환하는 데 Javascript를 사용하려고합니다. 생각은 여러 XML 파일에서 테이블에 여러 행을 만들 것입니다. 파이어 폭스와 오페라의 경우이 멋진 코드 조각이 아름답게 작동합니다.IE에서 조각을 처리하는 XSL

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"부분에서 수천 가지를 시도했지만 아무 것도 작동하지 않는다는 것입니다. 나는 많은 인터넷 검색을했고, 내가 물기 전에 여기에서 브라우징했다. 그러나 아무 소용이 없다. 실제로, 매우 유사하게 들릴지도 모르는, 의문스러운 질문이 있습니다. 그러나 응답이없고 해결이 없었기 때문에 누군가가 나를 도울 수 있기를 바랬습니다 ..

나는 전체 문서를 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

ActiveX 필터가 활성화되어 있어도 'true'로 평가되는 'window.ActiveXObject'로 IE를 탐지하면 안된다. 나중에 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>과 관련이 있으며, 테이블 태그의 컨텍스트 외부에서 innerHTML로 설정하면 구문 분석 할 수 없습니다.

어쨌든 임시 div 요소를 만들고 ITs innerHTML을 < 테이블> </table> 태그로 묶인 xsltProcessor의 출력 문자열을 갖는 문자열로 설정했습니다. 이제 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); 

을 사람들은 종종 이것을하려고 노력하지만 잘하면이 방법은 다른 사람을 돕는다. ..