Hello Monks. I've gotten a lot more familiar with twig now and I'm glad I stuck with it. I'm hitting a snag with 'ancestors' now. Here's what I've got
#!/bin/perl use strict; use warnings; use XML::Twig; my %Items; my $twig=XML::Twig->new( twig_handlers => {_all_ => sub {my $ancestors = ($_->ancestors); if ($ancestors =~ m/us:ItemMasterHeader/ ) {my $element_match = ($_->tag); my $coupled = join( ' - ' => $element_match,keys %{$_->atts},values %{$_->atts}),; if (!defined $Items{$coupled}){$Items{$coupled}=1} else {$Items{$coupled}++;} } }, #If element is not in the hash, adds it #If element is in the hash, adds the number of matches to the value print "\n Elements \n \n"; foreach my $k (sort keys %Items) {print "$k => $Items{$k}\n";};
What I'm aiming for is a printout of all the descendants of Item master header and their attributes along with how many times each one occurs in the document. From what I gather on Twig, ancestors should return the tag of the ancestors of the element in list context. Instead I'm getting a printout of numbers between 1 and six. Any suggestions? Anything wrong with my code? Does ancestors return a count of ancestors?

In reply to xml twig ancestors not returning a list of ancestors by Mr.Churka

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.