Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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]

In reply to Re: XML::LibXML + XML::LibXML::XPathContext: can this be simplified? by choroba
in thread XML::LibXML + XML::LibXML::XPathContext: can this be simplified? by Darkwing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2020-01-23 18:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?