open file.xml ; $version := hash ../id /objects/obj/version ; ls /objects/obj[ count(refs/id) and count(refs/id[xsh:lookup('version', .) = ../../version]) = count(refs/id) ]; #### #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::LibXML; use XML::LibXML::XPathContext; { my %hash; sub hash { my ($var, $dom, $key, $value) = @_; for my $node ($dom->findnodes($value)) { $hash{$var}{ $node->findvalue($key) } = $node; } } sub lookup { my ($var, $key) = @_; $hash{$var}{$key} } } my $dom = 'XML::LibXML'->load_xml(...); my $xpc = 'XML::LibXML::XPathContext'->new($dom); $xpc->registerFunction('lookup', \&lookup); hash(version => $dom, '../id', '/objects/obj/version'); my @nodes = $xpc->findnodes( '/objects/obj[ count(refs/id) and count(refs/id[lookup("version", .) = ../../version]) = count(refs/id) ]'); foreach my $node (@nodes) { say $node->toString(1); }