in reply to Twig / Simple / xmlgrep --help

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, }, ...

Replies are listed 'Best First'.
Re^2: Twig / Simple / xmlgrep --help
by Anonymous Monk on Aug 16, 2007 at 06:45 UTC
    Thanks alot for the suggestion. Actually I have so many XML files in a directory, and I cannt use element name(title) in the code. I want my script to be generic.
    --
    Best Regards,
    Cherry

      Then build the ruleset in the script, something like:

      use XML::Rules; @ARGV == 5 or die "Usage: BookStore3.pl roottag datatag idtag fitertag + filtervalue\n"; my ( $roottag, $datatag, $idtag, $fitertag, $filtervalue) = @ARGV; my $parser = XML::Rules->new( rules => [ _default => 'content', $datatag => sub { return unless $_[1]->{$fitertag} eq $_[4]->{parameters}; my $id = delete $_[1]->{$idtag}; delete $_[1]->{'_content'}; return $id => $_[1] }, $roottag => 'pass no content', ] ); my $data = $parser->parse(\*DATA, $filtervalue); use Data::Dumper; print Dumper( $data); __DATA__ <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore>
      and called as
      BookStore3.pl bookstore book title category CHILDREN

        Got your point sir..and lemme thanku coz this is getting easier for me a lil bit!!

        But at the CmdLine it should have been

        root@localhost ~# perl BookStore3.pl CHILDREN
        In response our script should take the 'title' in that particular 'category' for looking up in the another text file and output (at STDOUT) the entire matched/grepped entry.

        Is this possible with XML::Rules alone?
        --
        Best Regards,

        Cherry