ramya2005 has asked for the wisdom of the Perl Monks concerning the following question:
I have the following two XML files.
books.xml
filter.xml<Books> <Titles Mode="List"> <Title Name="Programming Perl"/> <Title Name="Advanced Perl Programming"/> <Title Name="Learning Perl on Win32 Systems"/> </Titles> </Books>
I am trying to do the following using the code below.<Filters> <Titles> <Title Name="Programming Perl"/> <Title Name="Advanced Perl Programming"/> <Title Name="Learning Perl on Win32 Systems"/> </Titles> </Filters>
1. Change the 'Title Name' in 'books.xml' by appending the existing 'Title Name' with some random number.
2. Change the corresponding 'Title Name' in 'filter.xml'
use XML::Twig; # Learning XML testing functions ReadBooksInfo('books.xml','filter.xml'); sub ReadBooksInfo{ my $books_name = shift; my $filter_name = shift; my $filter_tree; my $books_tree= new XML::Twig( TwigHandlers => { Title => sub { my( $titletwig, $title_ele ) = @_; + my $titlename = $title_ele->att('Name'); my $unique_titlename = CreateUniqueFilterName($filterna +me); print "\nGenerated unique title name: ", $unique_title +name; print "\n"; $title_ele->set_att( Name => "$unique_titlename "); $filter_tree= new XML::Twig( TwigHandlers => { qq[Title[\@Name="$titlename"]] => sub { my ( $tree, $element ) = @_; $element->set_att( Name => "$unique_titlena +me" ); $element->print; } } ); $filter_tree->parsefile($filter_name); } } ); $filter_tree->parsefile( $books_name ); $filter_tree->print; $books_tree->print; } # Create unique filter names # =========================== sub CreateUniqueFilterName { my $user_filter_name = shift; my $lower=1000; my $upper=2000000; my $random = int(rand( $upper - $lower + 1 ) ) + $lower; my $unique_filter_name = $user_filter_name . "_". "$$". "_". "$^T" +. "_" .$random; }
Please tell me how to fix this?
Back to
Seekers of Perl Wisdom