xslt将xml转换为html

xslt将xml转换为html

问题描述:

我有2个文件,xml和xsl。我想使用xslt来获取xml数据并转换为html表格。下面的代码不适用于Chrome,我不知道为什么,关于如何正确使用xslt和/或xslt的替代方案的任何建议?xslt将xml转换为html

的test.xml

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="style.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>` 

style.xsl

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <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>` 

一件事,你的XSLT缺少开始标记为xsl:template指令:

<xsl:template match="/"> 

另一方面,Chrome在本地运行XSLT存在固有的问题 - 请参阅:https://*.com/a/3839054/3016153(和其他)。

+0

Bummer ...有关如何在不使用XSLT的情况下完成相同工作的任何建议? –

+0

我不认为你会找到一个类似的工具。尝试服务样式表 - 或者按照下面的解释设置浏览器:http://*.com/q/18586921/3016153 –

+0

很酷......谢谢! –

您可以在服务器端使用XSLT并直接提供HTML内容,这样您就不必担心浏览器。

我已经在java中做了它,但我很确定你也有其他语言的XSLT引擎。