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.
|
|---|