Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'm trying to write a script do the following:

  • Read in an .unl file of translations from one language to another
  • Read in a consistent-format XML file, and parse it
  • Apply the translations to some attributes and text elements in the file
  • Write the file back out, with all latin-1 characters escaped in numeric form (e.g 'é' -> 'é'

Currently, I'm trying to do this with XML::Twig, but only the attributes are coming out escaped properly, not the text.

Read in translations and construct twig:
open( TRANSLATIONS, $translations_file ) or die "Could not open '$tran +slations_file': $!"; while( <TRANSLATIONS> ) { if( my ($word, $translation) = split /\|/ ) { push( @translations, { match => qr/\b$word\b/i, replacement => $translation, } ); } } close( TRANSLATIONS ); my $twig= new XML::Twig( twig_handlers => { 'FOO' => \&foo }, pretty_print => 'indented', output_filter => 'safe', ); $twig->parsefile( $xml_file ); $twig->print;
Apply translations to parts of element 'foo':
sub foo { my ($t, $elt)= @_; # apply translations to the name, and text of all descendant nodes if( my $name = $elt->att('NAME') ) { $elt->set_att( 'NAME', apply_translations($name) ); } my @descendants = $elt->descendants( 'BAR' ); foreach my $descendant (@descendants) { if( my $text = $descendant->text ) { $descendant->set_text( apply_translations( $text ) ); } } } sub apply_translations { my $text = shift; foreach (@translations) { $text =~ s/$_->{match}/$_->{replacement}/g; } return $text; }
Input
<FOO="Duration"> <BAR>Duration</BAR> </FOO> <FOO NAME="Airport"> <BAR>Airport</BAR> </FOO>
--->
Output
<FOO="Dur&#233;e"> <BAR>Durée</BAR> </FOO> <FOO NAME="A&#233;roport"> <BAR>A&#40111;port</BAR> </FOO>

I've also tried using XML::SAX to do the same thing, but it barfs on the xml version/encoding line, complaining 'Only ASCII encoding allowed without perl 5.7.2 or higher. You tried: iso-8859-1'.
Unfortunately another application running on this machine (not mine, so don't ask me why) requires perl 5.6.0 or 5.6.1 , so upgrading perl would be problematic at the very least.

I'm somewhat loathe to start hacking on a new version using a different module without being sure it's support for latin-1 is better than those I've tried so far.

Does anyone have any experience of doing anything like this, and have recommendations/advice they'd care to share?


In reply to Latin-1 characters and XML by kilinrax

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found