There's no reason to go messing with someone else's namespace.
sub detach {
my( $node ) = @_;
$node->parentNode->removeChild( $node );
}
detach($node);
would work just as well.
Except it's not enough. It won't separate it from the document.
$ perl -MXML::LibXML -E'
my $node;
{
my $xml = "<root><foo><bar/></foo></root>";
my $doc = XML::LibXML->new->parse_string($xml);
($node) = $doc->findnodes("//bar");
$node->parentNode->removeChild($node);
}
{
my $doc = $node->ownerDocument;
say "owner=", $doc;
if ($doc) { say $_->nodeName for $doc->findnodes("//*"); }
}
'
owner=XML::LibXML::Document=SCALAR(0x817bcb8)
root
foo
You need to give the node a new document.
$ perl -MXML::LibXML -E'
my $foster_home = XML::LibXML::Document->new("1.0", "UTF-8");
my $node;
{
my $xml = "<root><foo><bar/></foo></root>";
my $doc = XML::LibXML->new->parse_string($xml);
($node) = $doc->findnodes("//bar");
$node->setOwnerDocument($foster_home);
}
{
my $doc = $node->ownerDocument;
say "owner=", $doc;
if ($doc) { say $_->nodeName for $doc->findnodes("//*"); }
}
'
owner=XML::LibXML::Document=SCALAR(0x832af38)
Note that transfers the node's children too.