use strict; use XML::Rules; my $parser = XML::Rules->new( rules => [ _default => '', # not interested in most tags nt => sub {$_[1]->{_content}}, # append the content to parent's content lhs => 'content', # add the content to parent's attribute hash, using key "lhs" rhs => sub {'.rhs' => $_[1]->{_content}}, # append the content to key "rhs" in the the parent's attribute hash prod => sub { my ($tag, $attrs, $context, $data, $parser) = @_; $parser->{pad}++; print clean("[$parser->{pad}] $attrs->{lhs} ::= $attrs->{rhs}")."\n"; return; }, ], start_rules => [ 'header' => 'skip', # no point in parsing the
and its content, storing the data and throwing it away 'spec' => sub {$_[4]->{pad} = 0; 1}, #initialization ] ); $parser->parsefile('REC-xml-19980210.xml'); sub clean { my( $string)= @_; $string =~ s/\xc2\xa0/ /sg; $string =~ s/\s+/ /g; $string=~ s{\s$}{}g; return $string; }