http://www.perlmonks.org?node_id=1019196

FreakyGreenLeaky has asked for the wisdom of the Perl Monks concerning the following question:

Evening robe-rufflers... I have next to zero experience with XML and XML::LibXML and I hope someone can shed some light on the following.

Basically, I want to remove some empty elements in XML.

<?xml version="1.0" encoding="UTF-8"?> <epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.o +rg/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:n +s:epp-1.0 epp-1.0.xsd"> <command> <update> <contact:update xmlns:contact="urn:ietf:params:xml:ns:contact-1. +0" xsi:schemaLocation="urn:ietf:params:xml:ns:contact-1.0 contact-1.0 +.xsd"> <contact:id>testaccount3</contact:id> <contact:add/> <contact:rem/>

I need to remove those last two empty elements, but I'm guessing the namespace stuff (or something) is a factor.

I've naively tried the following:

for my $remove ($frame->findnodes(q{/command/update/contact:update/con +tact:add})) { $remove->unbindNode; }

Edit: using the above findnodes() results in the error:

XPath error : Undefined namespace prefix error : xmlXPathCompiledEval: evaluation failed
But I must admit I'm clueless here and would appreciate some pointers.

Update:
I've been looking at the source which creates the XML:
/usr/lib/perl5/site_perl/5.8.8/Net/EPP/Frame/Command/Update/Contact.pm

sub new { my $package = shift; my $self = bless($package->SUPER::new('update'), $package); my $contact = $self->addObject(Net::EPP::Frame::ObjectSpec->spec(' +contact')); foreach my $grp (qw(add rem chg)) { my $el = $self->createElement(sprintf('contact:%s', $grp)); $self->getNode('update')->getChildNodes->shift->appendChild($e +l); } return $self; }
And I must confess to being mightily tempted to just remove add rem from the foreach. This works, but it makes me feel decidedly dirty - and I might need those elements in the future.

Solution:

my $p = $frame->getElementsByTagName('contact:update'); foreach my $n ($p->[0]->childNodes()) { $p->[0]->removeChild($n) if $n->toString(1) eq "<contact:add/> +"; }
Not very elegant, but it works. If someone knows of a better way, I'd love to hear it.