Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: XML::LibXML + XML::LibXML::XPathContext: can this be simplified?

by choroba (Archbishop)
on Oct 08, 2019 at 15:14 UTC ( #11107207=note: print w/replies, xml ) Need Help??


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]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11107207]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2019-12-11 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?