#!/usr/bin/perl

use XML::Parser;

$p = new XML::Parser(Handlers => {Start => \&handle_start,
                                  End   => \&handle_end,
                                  Char  => \&handle_char});
$p->parsefile($ARGV[0]);


$skipped = 0;
$open = 0;
$content = "";
$uri = "";

sub handle_start
{
   my ($expat, $element, @attrs) = @_;
   if ($skipped == 1) {
      $uri = $attrs[1];
      $uri =~ s/'/\\'/g;
   }
   if ($skipped < 2) { $skipped++ ; }
   else {
      $open++;
      $content = "";
   }
}

sub handle_end
{
   my ($expat,$element) = @_;
   $open--;
   if ($open >= 0) {
      $content =~ s/'/\\'/g;
      $element =~ s/://g;    # dc:pippo ==> dcpippo
      print "insert into $element values ('$content','$uri');\n";
   }
}

sub handle_char
{
   my ($expat,$char) = @_;
   if ($open >= 1) {
      chomp($char);
      $char =~ s/ +/ /g;
      $char =~ s/^ //g;
      $char =~ s/ $//g;
      $content .= $char;
   }
}
