# UTF-8 Converter to ISO-8859-1
# Converts an XML-File encoded in UTF-8 to ISO-8859-1
# Reads from stdin, writes to stdout so it can be piped easily

use Unicode::String qw(utf8 latin1);
use strict;

my $line;
my $utf_line;

foreach $line (<>) {
  if ($line =~ /<\?xml.+encoding\s*=\s*["']\s*UTF-8.*/i) {
    $line =~ s/UTF-8/ISO-8859-1/i;
    print "$line";
  }
  else {
    $utf_line = utf8($line);
    print $utf_line->latin1;
  }
}
