添加XSL命名空间后没有元素

时间:2012-07-22 18:02:11

标签: xslt

对于这个简单(我希望)的问题,我们将不胜感激。在向此XML文档添加命名空间后,xsl:for-each无法找到任何元素:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="cdsort.xsl"?>
    <catalog xmlns="http://www.mycompany.com/test" >
      <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
      </cd>
      <cd>
        <title>Tupelo Honey</title>
        <artist>Van Morrison</artist>
      </cd>
    </catalog>

这是XSL代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:vv="http://www.mycompany.com/test"
    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>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="vv: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>

1 个答案:

答案 0 :(得分:0)

您已为整个文档设置默认命名空间,因此xpath中的子元素也需要前缀:

<xsl:for-each select="vv:catalog/vv:cd">
  <tr>
    <td><xsl:value-of select="vv:title" /></td>
    <td><xsl:value-of select="vv:artist" /></td>
  </tr>
</xsl:for-each>