#!/usr/bin/perl -w
# Author: Michel Rodriguez <m.v.rodriguez@ieee.org>
 
use strict;
use XML::XPath;
use XML::XPath::XMLParser;
 
my $xpp = XML::XPath::XMLParser->new(filename => 'REC-xml-19980210.xml');
my $tree = $xpp->parse;                            # build the XPATH tree structure
my $xp = XML::XPath->new(context => $tree);        # xp is the xpath object

my @prods = $xp->find("//prod")->get_nodelist; 

my $i=0;
foreach my $prod (@prods) 
#{ my $lhs = $xp->find('string(./lhs/text())', $prod);
  { my $lhs = $xp->find('./lhs', $prod)->to_literal;
    my $rhs = $xp->find('./rhs', $prod)->to_literal;

    $i++;
    my $prod_text= "[$i] $lhs ::= $rhs\n";
    $prod_text= clean( $prod_text);
    print $prod_text, "\n";
  }

sub clean { 
        my( $string)= @_;
        $string =~ s/\xc2\xa0/ /sg;
        $string =~ s/\s+/ /g; $string=~ s{\s$}{}g;
        return $string;
}
