#!/usr/bin/perl -w
# Author: Michel Rodriguez <m.v.rodriguez@ieee.org>
 
use strict;

use XML::LibXSLT;
use XML::LibXML;

my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();

my $source = $parser->parse_file('REC-xml-19980210.xml');
my $style_doc = $parser->parse_file('ex_ps_xslt.xslt');

my $stylesheet = $xslt->parse_stylesheet($style_doc);

my $results = $stylesheet->transform($source);

set_filter();    # sets the post-processing filter
print $stylesheet->output_string($results);


# filter applied to the output of the XSLT transformation
sub set_filter
  { return if my $pid= open( STDOUT, "|-");    # magic open
    die "cannot fork: $!" unless defined $pid;
    $|=1;
    my $prod='';
    while( <STDIN>)
      { if( m/^\s*\[\d+\]/) # found the beginning of a production
          { print( clean($prod), "\n") if( $prod=~ /\S/);
            $prod= '';
          }
          $prod .= $_;
        }
    print clean( $prod),"\n";
    exit;
  }
        
sub clean { 
        my( $string)= @_;
        $string =~ s/\xc2\xa0/ /sg;
        $string =~ s/\s+/ /g; $string=~ s{\s$}{}; $string=~ s{^\s}{};
        return $string;
}
