XML::LibXML seems to be doing strange things to UTF-8 encoded strings.

use strict; use warnings; use Encode qw(encode); use XML::LibXML; my $uchar = chr(195) . chr(154); my $xml = '<?xml version="1.0" encoding="UTF-8"?> <container><node>' . $uchar . '</node></container>'; output($uchar); my $dom = XML::LibXML->load_xml(string => $xml); my $node = $dom->findnodes('/container/node')->to_literal; output($node); my $encoded = encode('UTF-8', $node); output($encoded); sub output { my $str = shift; print "$str\n"; for (1..length($str)) { print ord(substr($str, $_-1)), ': '; } print "\n"; }

Some of my output is below. I have removed the lines printing the characters as that would involve more rendering issues.

195: 154: 218: 195: 154:

My real case is reading files, but I am getting the issue demonstrated in this example. The character I have chosen is one that is causing problems (a U with an acute accent), but other characters are being transformed as well.

Given that the XML is flagged as being UTF-8, I cannot see anything in the docs indicating why this transformation should take place. What have I missed, please?

Regards,

John Davies


In reply to UTF-8 and XML::LibXML by davies

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.