2012-04-23 6 views
1

私は変換されたXMLデータをXSL変換を使ってHTMLページに入れようとしています。HTMLデータが取り除かれ、プレーンテキストのみが残っているのはなぜですか?

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="html" /> 
<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Artist</th> 
     </tr> 
     <xsl:for-each select="catalog/cd"> 
     <tr> 
     <td><xsl:value-of select="title"/></td> 
     <td><xsl:value-of select="artist"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

しかし、変換されたXMLは、HTML属性を持っていません。:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="w3.xsl"?> 
<catalog> 
    <cd> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <year>1985</year> 
    </cd> 
    <cd> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </cd> 
    <cd> 
     <title>Greatest Hits</title> 
     <artist>Dolly Parton</artist> 
     <country>USA</country> 
     <company>RCA</company> 
     <price>9.90</price> 
     <year>1982</year> 
    </cd> 
</catalog> 

XSLファイルの内容:ここに私のXMLでありますそれは次のようになります。

それは次のようになります。

//Load the XML document and XSLT document into XML DOM objects 
var xml = document.implementation.createDocument("", "", null); 
var xslt = document.implementation.createDocument("", "", null); 

//Set the async property on both documents to false so that they both completely load 
//before any further processing is attempted. 
xml.async = false; 
xslt.async = false; 

//Load XML and XSLT documents into the XML DOM objects. 
xml.load("w3.xml"); 
xslt.load("w3.xsl"); 

//Create a new XSLTProcessor object and use its importStylesheet() method to import the XSLT DOM object. 
var processor = new XSLTProcessor(); 
processor.importStylesheet(xslt); 

//Transform the XML to a new XML DOM object with the transformToDocument() method of the XSLTProcessor. 
var XmlDom = processor.transformToDocument(xml) 

//Create a new XMLSerializer and use it to serialize the new XML DOM object to a string. 
var s = new XMLSerializer(); 
var output = s.serializeToString(XmlDom.documentElement); 

//The result can then be output to the innerHTML property of any element on the page. 
var outputDiv = document.getElementById("xmldata"); 
outputDiv.innerHTML = output; 

ここ

はDOMを埋める機能ですが、これが問題の原因であります

+1

次の画像は、実行結果を示しています。これは、出力がhttp://imgur.com/mzVOp,ilVgjのようになります方法です#0とhttp://imgur.com/mzVOp,ilVgj#1は実行後の様子です。 – Demego

+0

HTMLを表示しているアプリケーションを認識できません.HTMLテーブルの属性をレンダリングしてもよろしいですか? –

+0

これはfirebugアドオンを持つfirefoxです。あなたは画像のログコンソールを参照してください – Demego

答えて

0

あなたの「ホスト」HTMLページにはHTMLをHTML5として識別するDOCTYPEが必要です。

は、あなたが(W3C: HTML5 differences from HTML4を参照)HTML 5で廃止され、このようなBGCOLORなどの属性を追加しようとしているので、その後、それらは無視しているため、DOMに追加されません。

リンク先ページで推奨されているように、代わりにスタイル属性を使用してみてください。

tr要素は次のようになりためのXSLT:

<xsl:template match="/"> 
... 
    <tr style="background-color:#9acd32;"> 
    <th>Title</th> 
    <th>Artist</th> 
    </tr> 
... 
    </xsl:template> 
+0

私は原因を見つけた、CSSがエラーを引き起こしたようだ、まだそれをデバッグ、それは非常にあいまいです。 – Demego

+0

@Demegoあなたは、HTML5仕様で記述されている属性を「廃止」としているという事実とは関係ないと言っていますか? – pgfearo

+0

はい、私はそれが変換だと思うように、誤解を招くような方法でテーブルを変更したCSSプロパティでした。廃止された属性は正常に機能します。 – Demego

関連する問題