foreach my $child ($obj->children()) { next unless $child->name eq 'paragraph'; # code goes here } #### foreach my $child ($obj->children()) { next unless $child->name eq 'page'; foreach my $grandchild ($child->children()) { next unless $grandchild->name eq 'header'; # code goes here } } #### foreach my $para ($obj->match('paragraph')) { # code goes here } #### foreach my $header ($obj->match('/page/header')) { # code goes here }