use strict; use XML::Twig; my $sel = 'title'; my $ign = 'title/title/title'; my $twig = XML::Twig->new( ignore_elts => { "$ign" => 1}, twig_handlers => { "$sel" => \&title}, pretty_print => 'indented' ); $twig->parsefile('1.xml'); $twig->print; sub title { my ($twig, $tit) = @_; $tit->set_gi('tit'); } #### input: testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name><g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk> testing<name><snm>Houston</snm>, <fnm>Allan</fnm></name><g>69</g><ppg>20.1</ppg><rpg>3.4</rpg><apg>2.8</apg><blk>14</blk> <name1>Raja m</name1><g>49</g> <title> testing the sub title in title this is pcdata in subtitle <title> second sub level #### output: testingHouston, Allan6920.13.42.814 testingHouston, Allan6920.13.42.814 Raja m 49 testing the sub title in title this is pcdata in subtitle #### Expected output: testingHouston, Allan6920.13.42.814 testingHouston, Allan6920.13.42.814 Raja m 49 testing the sub title in title this is pcdata in subtitle second sub level