package Ernad::Rif::Truncate; use strict; use warnings; ## FixMe: should be Ernad::Rif; use base 'Ernad'; use Carp qw(confess); use Ernad::Common; my $xpc = &Ernad::Common::create_xpc; ## truncate by deleting from the current doc sub by_delete { my $t=shift; my $doc=shift; if(not ref($doc) eq 'XML::LibXML::Document') { confess 'I need an XML document here.'; } my $truncate_param=shift // confess 'I need a truncation parameter here.'; my $max_items=0; my $max_percent=0; if($truncate_param) { if($truncate_param=~m|^\d+$|) { $max_items=$truncate_param; if($max_percent<100) { warn "You want to truncate to $max_items less than 100, did you mean $max_items%?"; } } elsif($truncate_param=~m|^(\d+)%$|) { $max_percent=$1; if($max_percent>100) { confess "My percentage can not be larger than 100."; } } else { confess "You truncate parameter '$truncate_param' is neither a number nor percentage."; } } my $item_xp='/amf:amf/amf:collection[1]/amf:haspart'; my @item_eles=$xpc->findnodes($item_xp,$doc)->get_nodelist(); if(not $max_items) { $max_items=int(scalar @item_eles * $max_percent / 100); } my $count=$max_items; while($item_eles[$count]) { my $this_item=$item_eles[$count]; $this_item->parentNode->removeChild($this_item); $count++; } return $doc; } 1;