in reply to Re^2: how to display descendants in XML with Perl's XML::Twig
in thread how to display descendants in XML with Perl's XML::Twig

I don't understand. Can you post a bit bigger input I can test my code against? Use the <readmore> tags to save readers from excessive scrolling.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^4: how to display descendants in XML with Perl's XML::Twig
by Ppeoc (Beadle) on Oct 16, 2015 at 16:12 UTC
    Updated the question. I hope this makes more sense. Sorry I cant display a bigger part of the code because of IP
Re^4: how to display descendants in XML with Perl's XML::Twig
by Ppeoc (Beadle) on Oct 16, 2015 at 17:35 UTC
    Edited the question. Hope it makes more sense
      Try
      #!perl use strict; use XML::Twig::XPath; my %record = (); my $twig= new XML::Twig::XPath( twig_handlers => { qr/^Line[1-5]|Author|Publisher|Book|Snap/ => \&line, qr/^C\d+/ => \& rating, }, ); $twig->parsefile( 'book.xml' ); sub line { my ($t,$e) = @_; $record{$e->name} = $e->text; if ($e->name eq 'Publisher'){ report(\%record); %record=(); } } sub rating { my ($t,$e) = @_; my $parent = $e->name.'.'; for ($e->descendants){ if ($_->name =~ /[XY]/){ push @{$record{$parent.$_->name}},$_->text; } } } sub report { my $hr = shift; my @f = qw(Book Snap Line1 Line2 Line3 Line4 Line5); my $line = join '|',@$hr{@f}; for my $key (sort keys %$hr){ if ($key =~ /^C\d+/){ print join '|',$line,$key,@{$hr->{$key}}; print "\n"; } } }
      poj
      It's still not fully clear, but here's what I got from it:

      Output:

      The book of pages||The Beginning|We ceased to exist|Accept it|Now we l +ive|We reject it|C1.X|10.5|3.5|10.5|10.5|10.5|10.5 The book of pages||The Beginning|We ceased to exist|Accept it|Now we l +ive|We reject it|C1.Y|11.4|13.4|11.4|11.4|11.4|11.4 ...
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ