0

当我申请波纹管xslt时: -

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//cd[year=2988]">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

到下面的xml:

    <catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>2988</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>

我得到以下结果:

<html>
       <body>
          <h2>My CD Collection</h2>
          <table border="1">
             <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
             </tr>
             <tr>
                <td>Empire Burlesque</td>
                <td>Bob Dylan</td>
             </tr>
          </table>
       </body>
    </html>
                Greatest Hits
        Dolly Parton
        USA
        RCA
        9.90
        1982

我无法理解为什么

    Greatest Hits
    Dolly Parton
    USA
    RCA
    9.90
    1982

在html标签结束后出现。应用模板并形成表格,但为什么数据再次出现在结果中我是xslt的新手。请建议

4

1 回答 1

0

我想<xsl:apply-templates/>会惹麻烦......它正在调用标准的内置模板。尝试删除它。

编辑:对于输入 xml

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>2988</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>

xslt 应该遵循

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates select="//cd[year=2988]" />
    </xsl:template>

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

结果

<html>
    <body>
        <h2>My CD Collection</h2>
        <table border="1">
            <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
            </tr>
            <tr>
                <td>Empire Burlesque</td>
                <td>Bob Dylan</td>
            </tr>
        </table>
    </body>
</html>
于 2013-07-12T18:09:09.530 回答