I know you did not ask, but ... ;-) You could use XML::Rules to extract, massage and filter the data to make it easy to use later. This will read the XML and create a hash indexed by the book title:
This adds the filtering:use XML::Rules; my $parser = XML::Rules->new( rules => [ _default => 'content', book => sub { my $title = delete $_[1]->{title}; delete $_[1]->{'_content'}; $title => $_[1], }, bookstore => 'pass no content', ] ); my $data = $parser->parse(\*DATA); use Data::Dumper; print Dumper( $data); __DATA__ <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> ...
Once you have this you can parse the text file and put the data together.use XML::Rules; my $parser = XML::Rules->new( rules => [ _default => 'content', book => sub { return unless $_[1]->{category} eq $_[4]->{parameters}; my $title = delete $_[1]->{title}; delete $_[1]->{'_content'}; $title => $_[1], }, bookstore => 'pass no content', ] ); my $category = $ARGV[0] or die "Usage: BookStore2.pl category\n"; my $data = $parser->parse(\*DATA, $category); use Data::Dumper; print Dumper( $data); __DATA__ <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> ...
P.S.: Do not let the $_[1]->{tag_or_attr_name} and $_[4]->{parameters} scare you. I was just lazy to assign the parameters to the unnamed subroutine. With them named it would look like this:
... book => sub { my ($tag, $attr, $context, $parents, $parser) = @_; return unless $attr->{category} eq $parser->{parameters}; my $title = delete $attr->{title}; delete $attr->{'_content'}; $title => $attr, }, ...
In reply to Re: Twig / Simple / xmlgrep --help
by Jenda
in thread Twig / Simple / xmlgrep --help
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |