XML文件数据未呈现为html表

时间:2017-11-18 19:28:46

标签: html xml xslt

我希望XML数据呈现为HTML表格。但是,它不会只渲染文本值。救命!这里是代码,

<?xml-stylesheet type="text/html"  href="#style1"?>
<doc>
<head>
    <xsl:stylesheet version="1.0" id="style1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="id('xx')">
            <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>
</head>
<body>
    <catalog id="xx">
      <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>
</body>

输出仅以简单文本形式出现。为什么不在html表中呈现?如何使其呈现为HTML表格。

此致

1 个答案:

答案 0 :(得分:0)

您希望嵌入式样式表使用哪种浏览器? type="text/html"应该是type="text/xsl"才有意义,那么如果要在XML中使用ID属性,则需要在DOCTYPE中声明它们。

以下是适用于Firefox和Chrome的示例,我不认为IE或Edge支持嵌入式样式表:

<?xml-stylesheet type="text/xsl"  href="#style1"?>
<!DOCTYPE doc [
  <!ATTLIST xsl:stylesheet
     id ID #IMPLIED>
]>
<doc>
<head>
    <xsl:stylesheet version="1.0" id="style1" 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="doc/body/catalog[@id = 'xx']/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>
</head>
<body>
    <catalog id="xx">
      <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>
</body>
</doc>

https://martin-honnen.github.io/xslt/2017/test2017111804.xml