sub escape_with_encode { return $_[0] unless $_[0] =~ /[\x80-\xff]/; my $s = shift; Encode::from_to($s, "utf-8", "ascii", Encode::FB_XMLCREF); $s =~ s/&#x(....);/\\u$1/g; return $s; }