使用XSLT将XML转换为XHTML

1.示例

cdcatalog.xml

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

cdcatalog.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Title</th>
      <th style="text-align:left">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>

2.遇到的问题


用浏览器直接打开cdcatalog.xml,显示一片空白。
去掉这一行<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>就好了。

显然这不是我们想要的效果,我们希望XSLT能将XML转换为HTML。

3.解决问题

于是,尝试把cdcatalog.xmlcdcatalog.xsl都放到服务器上就好了!

参考:

posted @ 2020-08-20 11:20  gzhjj  阅读(208)  评论(0编辑  收藏  举报