我正在关注this link 我想将 xml 转换为 csv 我有一个 xml 并为其编写了 xsl 使用 href 来打开 xml,指向 Internet Explorer 上的 xsl 工作 但运行 this link 中的代码返回错误 无法解析命名空间前缀:xmlns
应该怎样解决它?
我有一个有效的 xml 和 xsl
xml
<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type="text/xsl" href="student2.xsl"?>
<rankings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ign="http://api.DomainName.com/2.0" count="438" total="438" offset="0" xsi:schemaLocation="http://api.DomainName.com/2.0 http://api.DomainName.com/2.0/api.xsd" >
<ranking keyword="80s fancy dress">
<ranks>
<rank week="201526" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201527" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201528" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201529" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201530" country="uk" searchengine="google_uk_en">NR</rank>
<rank week="201531" country="uk" searchengine="google_uk_en">NR</rank>
</ranks>
</ranking>
</rankings>
<小时/>
xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://api.DomainName.com/2.0" exclude-result-prefixes=xmlns>
<xsl:output method="text" encoding="UTF-8" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
</tr>
<xsl:for-each select="rankings/ranking/ranks/rank">
<tr>
<keyword><xsl:value-of select="../../@keyword"/></keyword>
<xsl:text>,</xsl:text>
<week><xsl:value-of select="@week"/></week>
<xsl:text>,</xsl:text>
<country><xsl:value-of select="@country"/></country>
<xsl:text>,</xsl:text>
<searchengine><xsl:value-of select="@searchengine"/></searchengine>
<xsl:text>,</xsl:text>
<rank><xsl:value-of select="../rank"/></rank>
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
java代码
从上面的链接运行java代码后 我收到错误
(Location of error unknown)org.xml.sax.SAXException: Can not resolve namespace prefix: xmlns
Exception in thread "main" java.lang.NullPointerException
at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1171)
at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1060)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1268)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1251)
最佳答案
what should fix it ?
要消除该错误,您需要删除以下内容:
exclude-result-prefixes=xmlns
来自您的 xsl:stylesheet
标记。您还需要删除默认 namespace 声明:
xmlns="http://api.DomainName.com/2.0"
否则你的所有输出都将被放置在该命名空间中,如果你希望它是 HTML,那么你肯定不希望发生这种情况(看起来)。
您还需要进行其他更改 - 例如,如果您想输出 HTML 表格,请将输出方法设置为“html”而不是“text”,并确保您的表格结构有效。
<小时/>注意:
您的 XML 声明了一个命名空间:xmlns:ign="http://api.DomainName.com/2.0"
但此命名空间声明未在任何地方使用。因此您的样式表可以忽略它。前提是您向我们展示了真实 XML 的代表性示例。
关于java - 使用 xsl java 将 xml 转换为 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33666641/