2011-07-28 8 views
0

私は以下の3つのファイルを持っています。これは、xmlファイルとxslファイルを取り出して処理し、結果をブラウザに表示することです。しかし、今問題は、私はクロムとIE7でHTMLをダブルクリックすると、私はすべて空白のページを持っています。誰がそれがなぜこのようなものなのか、そしてxsltファイルを実行して結果をWebページに表示する正しい方法は何かを教えてください。どうもありがとう。Webページで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ファイルは必要ありません。私は、あなたが探しているまさにカバーと信じてブログ記事にあなたを指しますが、ここでのコードを貼り付けるの代わりに

@Originalポスター :

0

Flynn1179 @、私はOPは、この使用してHTMLをしたいと仮定します以下のために:http://johanlouwers.blogspot.co.uk/2011/05/render-xml-into-div-using-ajax-and-xsl.html

注:あなたが例を実行した場合、私は、リンクがクリックされたときに起こっを指摘でWebStrom結果などのIDEからtest.htmlというファイルを起動発見したようそれは、直接お使いのブラウザを使用してください。これは、毎回空白のページを表示させていた原因かもしれません。

これがあなたの質問に答える(またはそれに最も近い)場合、この投稿を解決済みとしてリストアップできるように、それを選択された回答としてマークしてください。

あなたの問題の解決策を作成するためにブログ投稿のHTMLファイルにコードをどのように適応させるかが不明な場合は、ここでコメントしてください。

関連する問題