#!/usr/bin/perl use strict; use warnings; use XML::LibXML; die "Usage: $0 file.xml bad_uid ...\n" unless ( @ARGV > 1 ); my $xml_file = shift; my %bad_uids = map { $_ => undef } @ARGV; my $xml = XML::LibXML->new; my $dom = $xml->parse_file( $xml_file ); my @kill_list; for my $document ( $dom->getElementsByTagName( "document" )) { my ( $uid ) = $document->getElementsByTagName( "uid" ); push @kill_list, $document if ( exists( $bad_uids{ $uid->textContent } )); } warn sprintf( "Removing %d document elements from %s\n", scalar @kill_list, $xml_file ); $_->unbindNode for ( @kill_list ); rename $xml_file, "$xml_file.old" or die "Unable to rename $xml_file\n"; open( NEW, ">", $xml_file ) or die "Unable to write new $xml_file\n"; print NEW $dom->toString; close NEW;