in reply to Re: Remove level of elements (preserving their children) in XML::Twig?
in thread Remove level of elements (preserving their children) in XML::Twig?
With PerlX::MethodCallWithBlock it can get even prettier...
use XML::LibXML 1.93; use XML::LibXML::PrettyPrint qw/print_xml/; use PerlX::MethodCallWithBlock; my $doc = XML::LibXML->load_xml(IO => \*DATA); $doc -> findnodes('//policyrules') -> foreach { my $elem = shift; $elem->parentNode->appendChild($_) foreach $elem->childNodes; $elem->parentNode->removeChild($elem); }; print_xml($doc);
It almost makes me want to weep.
I can get it down to a single chain, but then it starts looking a little obfuscated...
use XML::LibXML 1.93; use XML::LibXML::PrettyPrint qw/print_xml/; use PerlX::MethodCallWithBlock; XML::LibXML -> load_xml(IO => \*DATA) -> findnodes('//policyrules') -> foreach { my $elem = shift; $elem->parentNode->appendChild($_) foreach $elem->childNodes; $elem->parentNode->removeChild($elem); } -> map { $_->ownerDocument } -> foreach { print_xml $_ and exit };
|
|---|