in reply to Loop through multiple elements with same name in XPath
I know I'm getting tiresome ...
ormy $parser = XML::Rules->new( rules => { _default => 'content', category => 'content array', entry => 'array no content', post => 'pass no content', } ); my $data = $parser->parse($the_xml); foreach my $entry (@$data) { print "Title: $entry->{title}\nContent: $entry->{content}\n"; if ($entry->{category)[0] eq 'blog') { print "It's a blog post\n\n"; } ...
or if you like you can handle each the <entry> within the sub specified in the rules and forget the value then and save memory.my $parser = XML::Rules->new( rules => { _default => 'content', category => 'content array', entry => sub { my ($tag,$attr) = @_; delete $attr->{_content}; $attr->{type} = shift(@{$attr->{category}}); return '@entry' => $attr; }, post => 'pass no content', } ); my $data = $parser->parse($the_xml); foreach my $entry (@$data) { print "Title: $entry->{title}\nContent: $entry->{content}\n"; if ($entry->{type) eq 'blog') { print "It's a blog post with labels: ".join(', ', @{$entry->{categ +ory}})."\n\n"; } ...
Jenda
Enoch was right!
Enjoy the last years of Rome.
|
|---|