xsl with xml with root element中的属性不起作用

时间:2011-11-17 11:19:20

标签: xml xslt xpath

我使用sw生成一个xml文件,我想在html文件中显示该文件,所以我开始创建一个xsl文件来为我做这个。 问题是我不知道如何解决由于属性导致的错误列表根元素。如果我从xml文件中删除属性,则xsl可以正常工作。

我的xml文件是:

<errorList xmlns="http://www.klocwork.com/inForce/report/1.0" version="9.1.0">
<problem>
 <problemID>1</problemID>
 <file>stdafx.h</file>
</problem>
<problem>
...
</problem>
</errorList>

到目前为止我的xsl是:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h2>Issues</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>ProblemID</th>
        <th>File</th>
      </tr>
       <tr>
    <td><xsl:value-of select="errorList/problem/problemID"/></td>
    <td><xsl:value-of select="errorList/problem/file"/></td>    
        </tr>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

问题是,如果'errorList'标签中存在属性,则输出是一个没有行的表,但是如果我删除了属性,它就可以正常工作。

2 个答案:

答案 0 :(得分:4)

向XSLT添加名称空间声明:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:k="http://www.klocwork.com/inForce/report/1.0">

然后使用它:

<xsl:value-of select="k:errorList/k:problem/k:problemID"/>

答案 1 :(得分:3)

<xsl:stylesheet version="1.0"
    xmlns:k="http://www.klocwork.com/inForce/report/1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

然后将其引用为k:errorList