use warnings; use strict; my $xpath = 'BoringNode[1]/InterestingNode[@InterestingAttribute="outgrabe"]/AnotherBoringNode[5]'; $xpath =~ s|\/[^/]+$||; print $xpath . "\n";