我正在将 XML 文档转换为 HTML。需要做的事情之一是删除命名空间,命名空间不能在 HTML 中合法声明(除非它是根标记中的 XHTML 命名空间)。我发现过 5 到 10 年前的帖子,介绍使用 XML::LibXML 和 LibXML2 来实现这一点有多么困难,但最近没有那么多。这是一个例子:
use XML::LibXML;
use XML::LibXML::XPathContext;
use feature 'say';
my $xml = <<'__EOI__';
<myDoc>
<par xmlns:bar="www.bar.com">
<bar:foo/>
</par>
</myDoc>
__EOI__
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($xml);
my $bar_foo = do{
my $xpc = XML::LibXML::XPathContext->new($doc);
$xpc->registerNs('bar', 'www.bar.com');
${ $xpc->findnodes('//bar:foo') }[0];
};
$bar_foo->setNodeName('foo');
$bar_foo->setNamespace('','');
say $bar_foo->nodeName; #prints 'bar:foo'. Dang!
my @namespaces = $doc->findnodes('//namespace::*');
for my $ns (@namespaces){
# $ns->delete; #can't find any such method for namespaces
}
say $doc->toStringHTML;
在这段代码中,我尝试了一些不起作用的事情。首先我尝试设置名称bar:foo
元素到无前缀foo
(文档说该方法知道名称空间,但显然不知道)。然后我尝试将元素名称空间设置为 null,但这也不起作用。最后,我查看了文档以找到删除命名空间的方法。没有这样的运气。最终的输出字符串仍然包含我想要删除的所有内容(命名空间声明和前缀)。
有谁有办法删除命名空间,将元素和属性设置为空命名空间?
这是我自己的体操答案。如果没有更好的办法,就这样吧。我当然希望有更好的方法...
The replace_without_ns
方法只是复制没有命名空间的节点。任何需要命名空间的子元素都会获得其声明。下面的代码将整个文档移动到 null 命名空间中:
use strict;
use warnings;
use XML::LibXML;
my $xml = <<'__EOI__';
<myDoc xmlns="foo">
<par xmlns:bar="www.bar.com" foo="bar">
<bar:foo stuff="junk">
<baz bar:thing="stuff"/>
fooey
<boof/>
</bar:foo>
</par>
</myDoc>
__EOI__
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($xml);
# remove namespaces for the whole document
for my $el($doc->findnodes('//*')){
if($el->getNamespaces){
replace_without_ns($el);
}
}
# replaces the given element with an identical one without the namespace
# also does this with attributes
sub replace_without_ns {
my ($el) = @_;
# new element has same name, minus namespace
my $new = XML::LibXML::Element->new( $el->localname );
#copy attributes (minus namespace namespace)
for my $att($el->attributes){
if($att->nodeName !~ /xmlns(?::|$)/){
$new->setAttribute($att->localname, $att->value);
}
}
#move children
for my $child($el->childNodes){
$new->appendChild($child);
}
# if working with the root element, we have to set the new element
# to be the new root
my $doc = $el->ownerDocument;
if( $el->isSameNode($doc->documentElement) ){
$doc->setDocumentElement($new);
return;
}
#otherwise just paste the new element in place of the old element
$el->parentNode->insertAfter($new, $el);
$el->unbindNode;
return;
}
print $doc->toStringHTML;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)