Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.


In reply to How to remove empty XML elements using XML::LibXML by FreakyGreenLeaky

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (12)
    As of 2014-12-26 00:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (163 votes), past polls