java XML修复命名空间声明
我试图检测/解决RSS元素中的thisbug。 这意味着我必须找到一个错误的名称空间声明并更改其名称空间 值设置为正确的命名空间。例如:
xmlns:media="http://search.yahoo.com/mrss"
必须是:
xmlns:media="http://search.yahoo.com/mrss/"
在给定组织的情况下,我如何实现这一目标。w3c。文件
我的意思是我们发现了如何获取某个名称空间的所有元素:
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression expr = xpath.compile("//*[namespace-uri()='http://search.yahoo.com/mrss']");
Object result = expr.evaluate(d, XPathConstants.NODESET);
if (result != null) {
NodeList nodes = (NodeList) result;
for(int node=0;node<nodes.getLength();node++)
{
Node n = nodes.item(node);
this.log.warn("Found old mediaRSS namespace declaration: "+n.getTextContent());
}
}
所以现在我必须弄清楚如何通过JAXP更改节点的名称空间
# 1 楼答案
您可能可以使用XSLT,使用如下规则:
其中媒体绑定到“http://search.yahoo.com/mrss”
你可能需要稍微调整一下语法,因为我在没有编译器的帮助下写这篇文章。此外,您可能不会得到非常好的格式(许多元素上的名称空间声明),但它应该在位置上是正确的
# 2 楼答案
为了完整起见:
Java代码:
样式表:
特别感谢Mads Hansen对XSLT的help