XSL模板匹配返回意外数据

时间:2013-07-12 18:04:57

标签: xml xslt

当我申请bellow 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 .please advice

的新手

1 个答案:

答案 0 :(得分:0)

我猜<xsl:apply-templates/>会造成麻烦...它正在调用标准built-in 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>