Mr.Churka has asked for the wisdom of the Perl Monks concerning the following question:
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?#!/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";};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: xml twig ancestors not returning a list of ancestors
by eff_i_g (Curate) on Nov 14, 2007 at 18:24 UTC |