2011-07-28 43 views
0

我有以下三個文件,它應該做的是取出xml和xsl文件並根據它們處理它們並在瀏覽器中顯示結果。但現在的問題是,當我雙擊Chrome和IE7的HTML,我都得到了空白頁。任何人都可以告訴爲什麼它是這樣的,以及執行xslt文件並在網頁上顯示結果的正確方法是什麼?非常感謝。如何在網頁上執行並顯示xslt結果?

student.xml file 

<?xml version="1.0" encoding="ISO-8859-1"?> 

<data> 
<student> 
    <name>Bitu Kumar</name> 
    <course>MCA</course> 
    <sem>6</sem> 
    <marks>80</marks> 
</student> 
<student> 
    <name>Santosh Kumar</name> 
    <course>MCA</course> 
    <sem>5</sem> 
    <marks>70</marks> 
</student> 
<student> 
    <name>Ashish</name> 
    <course>M.Sc.</course> 
    <sem>4</sem> 
    <marks>80</marks> 
</student> 
<student> 
    <name>Mahesh</name> 
    <course>MA</course> 
    <sem>3</sem> 
    <marks>80</marks> 
</student> 
</data> 

and following student.xsl file 

<?xml version="1.0" encoding="ISO-8859-1"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
<html> 
    <body> 
    <h2>Student DataBase</h2> 
    <table border="1"> 
      <tr> 
      <th bgcolor="yellow">Name</th>  
     <xsl:for-each select="data/student"> 
         <td width="200"><xsl:value-of select="name"/></td> 
      </xsl:for-each> 
      </tr> 
      <tr> 
      <th bgcolor="yellow">Course</th>  
     <xsl:for-each select="data/student"> 
         <td width="200"><xsl:value-of select="course"/></td> 
     </xsl:for-each> 
      </tr> 
      <tr> 
      <th bgcolor="yellow">Marks</th>  
     <xsl:for-each select="data/student"> 
         <td width="200"><xsl:value-of select="marks"/></td> 
     </xsl:for-each> 
      </tr> 
      <tr> 
      <th bgcolor="yellow">Semester</th>  
     <xsl:for-each select="data/student"> 
         <td width="200"><xsl:value-of select="sem"/></td> 
     </xsl:for-each> 
      </tr> 
     </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

and following student.html file 

<html> 
<head> 
<title> Testing IE </title> 
<script langauge="JavaScript" type="text/javascript"> 
    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 displayResult() 
    { 
    xml=loadXMLDoc("student.xml"); 
    xsl=loadXMLDoc("student.xsl"); 
    // code for IE 
    if (window.ActiveXObject) 
    { 
     ex=xml.transformNode(xsl); 
     document.getElementById("example").innerHTML=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); 
     document.getElementById("example").appendChild(resultDocument); 
    } 
    } 
</script> 
</head> 

<body onLoad="displayResult()"> 
    <div id="example" /> 
</body> 
</html> 

回答

1

最簡單的方法是將樣式表指令放在XML文檔中,然後打開XML文檔。

剛開始你的XML文檔,像這樣:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="student.xsl"?> 
<data> 
<student> 
etc.. 

你甚至都不需要你student.html文件,同時這種方式。

0

@ Flynn1179,我假設OP要做到這一點使用HTML:

@Original海報 而是這裏粘貼代碼的,我會爲您的博客文章,我相信包括你在尋找什麼爲:http://johanlouwers.blogspot.co.uk/2011/05/render-xml-into-div-using-ajax-and-xsl.html

注:如果運行的例子,直接用它瀏覽器,因爲我發現從像WebStrom一個IDE啓動的test.html文件做的結果在被點擊注意到發生時的聯繫。這可能是你每次都得到一個空白頁面的原因。

如果這個回答您的問題(或最接近回答吧),請把它標記爲所選擇的答案使這個帖子可以被列爲解決。

如果你不清楚如何適應在博客中的HTML文件來創建解決你問題的代碼,只是在這裏評論,我將創建一個個性化的適應你。

相關問題