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:

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> ...
This adds the filtering:
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> ...
Once you have this you can parse the text file and put the data together.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.