davis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

NB: I've been living inside my ASCII shell for as long as possible, but I'm just starting to look at unicode data. My understanding of how to manipulate it is limited.

I've been using a perl script to turn a bunch of Microsoft Excel files into XML files, and from there, import them into a MySQL DB. Recently I've spotted some non-ASCII values in the Excel files, such as "ß". I decided that, since Perl's got Unicode support, I'll turn the contents of the spreadsheets into utf8, then import them into the database, and display the results in an HTML page via another Perl script.

I'm having trouble with this, and I'm not sure where the problem is. What I do:

  1. Run Encode's from_to on each data field to go from from "iso-8859-1" to "utf8"
  2. Put the data in a hash, and print with something very similar to the following:
my $xs = new XML::Simple(ForceArray => 1, KeyAttr => 1, KeepRoot => 1, + NoAttr => 1); print '<?xml version="1.0" encoding="UTF-8"?>', "\n"; print $xs->XMLout(\%foo, RootName => "foo");

Assumption Number 1: I'm assuming, at this point, my XML files will contain valid Unicode data (there's a couple of funny characters in place of the German "double-s" character). I do not know how to prove this, but the files are slurped in again with XML::Simple ok.

The data is then read in by another Perl script (using XML::Simple), and put into the database (MySQL 4.0.20, which may have dubious utf8 support -- I'm not sure). The column in question was declared of SQL type "varchar(255) character set utf8", and the field in the database looks the same (to me) as the value in the XML file.

Assumption Number 2: The utf8 data has made it into the database successfully.

I then want to display the same data (using HTML::Template) in an HTML file. The trouble is, even with a '<?xml version="1.0" encoding="UTF-8"?>' at the front, the HTML as displayed in my browser (and in the HTML source) isn't the correct character. It looks like it's the same "funny characters" as in the XML file and the database.

Assumption Number 3: I need to do something here to convert the value stored in the database to the HTML unicode entity.

Are my assumptions correct? If so, how do I get well-formed, valid, (and correct!) HTML in my browser?

Cheers


davis
It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.

Replies are listed 'Best First'.
Re: Mildly OT: Excel Order Forms -> XML -> MySQL -> HTML with unicode
by jZed (Prior) on Oct 06, 2004 at 15:21 UTC
    This suggestion is tangential to your unicode question, but has other advantages. Why not skip the XML and just use DBD::ODBC or DBD::Excel to read the Excel file directly? If you need to end up with XML in addition to the database and HTML, then you might be able to generate the XML from the database (or directly from the spreadsheet) using DBD::AnyData.

      Why not skip the XML and just use DBD::ODBC or DBD::Excel to read the Excel file directly?
      Because the data extraction process is complicated, and I've already done it. There are many guidelines to be stretched as needed, and there are a couple of different versions of these files, to which I've already built a uniform interface

      cheers anyway!


      davis
      It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: Mildly OT: Excel Order Forms -> XML -> MySQL -> HTML with unicode
by iburrell (Chaplain) on Oct 06, 2004 at 16:39 UTC
    You don't need to have character entities in the XML. XML parsers should handle Unicode characters in the document just fine. The encoding="UTF-8" signals the encoding of the XML to all XML parsers. On the web you will need to set the Content-Type correctly; including the charset if possible is a good idea.

      Thanks, this solved my problem, after some playing (and forgetting about the problem for a bit). When I was viewing the page containing the Unicode characters, I viewed "Page Information" in my browser. Turns out Apache was specifying the charset as ISO-8859-1 in its HTTP Response, and this was overriding my browser's interpretation of the Meta tags. A quick

      $q->charset("UTF-8");
      in my display code, and everything is fixed.
      Cheers!


      davis
      It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: Mildly OT: Excel Order Forms -> XML -> MySQL -> HTML with unicode
by Anonymous Monk on Oct 06, 2004 at 15:12 UTC
    perldoc perlunicode
    perldoc perluniintro