xsl with xml with attribute in root element does not work
我使用一个 sw 生成一个 xml 文件,并且我想将该文件呈现在一个 html 文件中,所以我开始创建一个 xsl 文件来为我做这件事。
问题是由于属性,我不知道如何解决错误列表根元素。如果我从 xml 文件中删除属性,则 xsl 工作正常。
我的 xml 文件是:
1
2 3 4 5 6 7 8 9 |
<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 是:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version=“1.0”?>
<xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“/”> <html> <body> Issues <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”标签中,则输出是一个没有行的表,但如果我删除属性它工作正常。
- 你已经有了很好的解决方案。有关说明,请参阅 en.wikipedia.org/wiki/XML_namespaces#Namespace_declaration 或搜索”XML 默认命名空间声明”。
- LarsH,这里的默认命名空间似乎是 html 之一。
- @MichaelKrelin-hacker:是的……我认为默认命名空间声明的解释仍然很有帮助。不是吗?
向 XSLT 添加命名空间声明:
1
2 |
<xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
xmlns:k=“http://www.klocwork.com/inForce/report/1.0”> |
然后使用它:
1
|
<xsl:value-of select=“k:errorList/k:problem/k:problemID”/>
|
1
2 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.
- 呃…版本=”9.1.0″?你来自未来? :-)
- 还有 version=”1.0″ :) 从 OP 的 xml 中复制了太多,谢谢,我会编辑它。
- 太糟糕了,我正要问你在 XSLT 9.1.0 中可以期待哪些特性。
- @eniac,是的,只是起初我用你的命名空间声明复制了它,而 LarsH 正在取笑我。
来源:https://www.codenong.com/8166231/