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

Edited the question. Hope it makes more sense
  • Comment on Re^4: how to display descendants in XML with Perl's XML::Twig

Replies are listed 'Best First'.
Re^5: how to display descendants in XML with Perl's XML::Twig
by poj (Abbot) on Oct 16, 2015 at 20:33 UTC
    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
Re^5: how to display descendants in XML with Perl's XML::Twig
by choroba (Cardinal) on Oct 18, 2015 at 20:31 UTC
    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 ...
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ