Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: XML::Twig how to find a parent's attribute

by poj (Priest)
on Jul 25, 2014 at 18:55 UTC ( #1095084=note: print w/ replies, xml ) Need Help??


in reply to XML::Twig how to find a parent's attribute

$elt->parent('p[@conref != ""]')

#!perl use strict; use XML::Twig; my $xml = do { local $/; <DATA> }; my $twig = new XML::Twig( twig_handlers =>{ li => \&li}, ); $twig->parse($xml); sub li { my ($t,$elt) = @_; print $elt->text; if ( $elt->parent('p[@conref != ""]') ){ print " yes\n"; } else { print " no\n"; } } __DATA__ <xml> <p conref="hello"> <ul> <li>Bob</li> <li>Mary</li> </ul> </p> <p conref=""> <ul> <li>Tom</li> <li>Susan</li> </ul> </p> </xml>
poj


Comment on Re: XML::Twig how to find a parent's attribute
Select or Download Code
Re^2: XML::Twig how to find a parent's attribute
by slugger415 (Beadle) on Jul 26, 2014 at 01:03 UTC

    Thanks poj, though I should have mentioned, I won't always know that the ancestor is a p; it could be anything. Anyway to grab "whatever it is"?

      sub li { my ($t,$elt) = @_; my $e = $elt->parent('[@conref != ""]'); # p removed if ( defined $e ){ print $e->tag." ".$elt->text."\n"; } }
      poj

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-12-28 04:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls