Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: XML cleanup - regex or ?

by ikegami (Patriarch)
on Sep 21, 2010 at 16:23 UTC ( [id://861105]=note: print w/replies, xml ) Need Help??


in reply to XML cleanup - regex or ?

XPath '//cat[not(@meow)]' will identify such nodes.

XML::LibXML example:

use strict; use warnings; use XML::LibXML qw( ); my $xml = <<'__EOI__'; <root> <cat tail='text' meow='text'/> <cat tail='text' meow=''/> <cat tail='text'/> <dog tail='text' bark='text'/> </root> __EOI__ my $doc = XML::LibXML->new()->parse_string($xml); my $root = $doc->documentElement(); for my $node ($root->findnodes('//cat[not(@meow)]')) { $node->setAttribute(meow => 'default'); } print $doc->toString();
<?xml version="1.0"?> <root> <cat tail="text" meow="text"/> <cat tail="text" meow=""/> <cat tail="text" meow="default"/> <dog tail="text" bark="text"/> </root>

Or if you prefer, $node->parentNode()->removeChild($node); would remove the offending node.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://861105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-25 18:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found