#!/bin/perl -w use strict; use XML::XPath; my @results; # create the xpath object from the DATA filehandle my $xp = XML::XPath->new( ioref => \*DATA); # find all paragraphs my $elts = $xp->find('/doc/elt'); foreach my $elt ($elts->get_nodelist) { # there is probably a more elegant way to get the child children # but I don't know XML::XPath enough my $children= $elt->getChildNodes; # get all children my @children= grep { $_->getName eq 'child' } @$children; # grep only the relevant ones if( @children) { push @results, $children[0]->string_value; } # that's how you get the text else { push @results, "missing child for elt " . $elt->getAttribute( 'id'); } } print join "\n", @results; print "\n"; __DATA__ I am a child 1 child 2