Code using XML::Simple rarely ends up being simple. It would be much easier if you used XML::LibXML...

#!/usr/bin/perl use feature ":5.14"; use warnings FATAL => qw(all); use strict; use XML::LibXML 2; my $x = XML::LibXML::->load_xml(string => <<'END'); <classes name="Panoply::BAR"> <public_methods> <members name="BAR" const="no" kind="function" protection="public +" static="no" virtualness="non_virtual" volatile="no"> <parameters declaration_name="pciReg" type="Register::Ptr" /> </members> </public_methods> <enums> <members name="ObjectState" kind="enum" protection="public" stati +c="no" virtualness="non_virtual"> <values name="NEW"> </values> <values name="REFRESHED"> </values> <values name="DIRTY"> </values> </members> </enums> </classes> END sub _universal_find { my $text = shift; join '|', "//$text", # Element name "//*/\@$text", # Attribute name "//*/text()[.='$text']", # Element contents "//*/\@*[.='$text']", # Attribute value } my $coolNodeName = sub { ($_[0]->isa('XML::LibXML::Attr')?'@':'').$_[0 +]->nodeName }; for my $text (qw[ parameters pciReg DIRTY ]) { say "Search for '$text'..."; for my $node ($x->findnodes( _universal_find($text) )) { say join ' => ', $node->findnodes("ancestor::*")->map(sub { $coolNodeName-> +($_) }), $coolNodeName->($node), } say ""; } __END__ Search for 'parameters'... classes => public_methods => members => parameters Search for 'pciReg'... classes => public_methods => members => parameters => @declaration_nam +e Search for 'DIRTY'... classes => enums => members => values => @name

(Update: improved _universal_find to return attribute nodes instead of element nodes when attribute nodes are more appropriate; improved output.)

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: sub that finds ancestor elements by tobyink
in thread sub that finds ancestor elements by jccunning

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, details, 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, summary, 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.