my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("main_file.XML"); my $doc1 = $parser->parse_file("second_file.XML"); #get my text my @nodes_from_main= $doc0->findnodes('//path/to/element/text()'); my @nodes_from_second = $doc1->findnodes('//path/to/element/text()'); #get the node containing the previous text i.e containing my @node_I_need_from_main= $doc0->findnodes('//path/to/'); my @node_I_need_from_second = $doc1->findnodes('//path/to/)'); #Note that main is unique meaning it will have only one instant from the text im matching #where my second, third, fourth....etc. will or might have more than one match #now im trying to find the match which is working fine my $i=0; for my $node_main (@nodes_from_main) { #print my node from the main file, next look for matches #the problem is im looping through nodes at //path/to/element/text() #dont know how to loop through '//path/to/' at the same time #or change my path at this particular instant for my $node_second (@nodes_from_second) { if (text from main equal text from second, "which I did already") { print my matching node from the second file, same issue I discussed in the above comment. Im looking for something like this: print $node_I_need_from_second [$i] (matching node) or any other way to get the same result i.e print node: matching text other text other text } esle { #do nothing; } ++$i; } }