XML 修复名称空间声明
我正在尝试检测/解决 RSS 元素中的 此 错误。 这意味着我必须找到错误的名称空间声明并更改其 值到正确的命名空间。例如:
xmlns:media="http://search.yahoo.com/mrss"
必须是:
xmlns:media="http://search.yahoo.com/mrss/"
给定 org.w3c.Document,我怎样才能实现这一目标?
我的意思是找到了如何获取某个命名空间的所有元素:
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 更改节点的命名空间。
I am trying to detetct/work around this bug in RSS elements.
That means I have to find a wrong namespace-declaration and change its
value to the correct namespace. E.g:
xmlns:media="http://search.yahoo.com/mrss"
must be:
xmlns:media="http://search.yahoo.com/mrss/"
How can I achive that given a org.w3c.Document?
I meanwile found out how to get all elements of a certain namespace:
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());
}
}
So now I have to figure out how to change the namespace of a Node via JAXP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 XSLT 来实现,规则如下:
其中媒体绑定到“http://search. yahoo.com/mrss”。
您可能需要稍微调整语法,因为我是在没有编译器帮助的情况下编写的。另外,您将得到的可能不是非常好的格式(许多元素上的命名空间声明),但它应该是本地正确的。
You could probably do it with XSLT, with a rule like this:
where media is bound to "http://search.yahoo.com/mrss".
You may have to tweak the syntax a little, as I'm writing this without the help of a compiler. Also, what you'll get is probably not extremely nicely formatted (namespace declarations on many elements), but it should be locically correct.
只是为了完整起见:
Java 代码:
样式表:
特别感谢 Mads Hansen 的帮助 使用 XSLT。
Just for the sake of completeness:
Java Code:
Stylesheet:
Special thanks to Mads Hansen for his help with the XSLT.