http://www.perlmonks.org?node_id=11107207


in reply to XML::LibXML + XML::LibXML::XPathContext: can this be simplified?

You might use XML::XSH2. It's not "another xml module", it's a wrapper around XML::LibXML which makes many things possible. For example, you can use its hash command to hash all the versions by object id, and later use its xsh:lookup function in an XPath expression to retrieve an element from the hash.
open file.xml ; $version := hash ../id /objects/obj/version ; ls /objects/obj[ count(refs/id) and count(refs/id[xsh:lookup('version', .) = ../../ve +rsion]) = count(refs/id) ];

Update: You can define the same hashing function yourself and solve your problem in the same way:

#!/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", .) = ../../vers +ion]) = count(refs/id) ]'); foreach my $node (@nodes) { say $node->toString(1); }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]