Joao Carlos
Joao Carlos

Reputation: 15

XML Find and replace Based on property

I need help with this little project, i have two files, one in Vietnam/Chinese and other in English, and i want to find the elements identical in both of them (5k+) and replace,

Example:

VIET: <Property name="GM_SUSSECC" content="đem ngươi {@}{@}" type="2"/>
ENG:  <Property name="GM_SUSSECC" content="{@}{@} Logged In" type="2"/>

So, I want to find the same name in english and replace with its content.Its like translating.

I tried using excel, but it seems it loose format and break my xml, So does anyone know a better way to do this?

Upvotes: 0

Views: 39

Answers (2)

Sobrique
Sobrique

Reputation: 53478

I'd probably tackle this in perl, using the rather excellent XML::Twig library. This comes bundled with Strawberry Perl

I can't give a full example without some more XML example, but:

#!/usr/bin/perl
use warnings;
use strict;

use XML::Twig;

my $source_eng = XML::Twig -> new -> parsefile ( 'test_eng.xml');
my $source_viet = XML::Twig -> new -> parsefile ( 'test_viet.xml'); 

foreach my $property ( $source_eng -> get_xpath('//Property') ) {
   my $name = $property -> att('name'); 
   my $first_match = $source_viet -> get_xpath("//Property[\@name=\'$name\']",0);

   if ( $first_match ) { 
       print "match found for $name\n";
       my $content = $first_match -> att('content'); 
       $property -> set_att('content', $content ); 
   }
}

$source_eng -> set_pretty_print('indented');
$source_eng -> print;

Upvotes: 0

choroba
choroba

Reputation: 241828

I'd use xsh. Create a hash table of the English contents keyed by the name, then replace it in the Vietnamese file:

open eng.xml ;
$eng := hash ../@name //Property/@content ;
open viet.xml ;
for //Property {
    my $viet = xsh:lookup('eng', @name) ;
    if $viet set @content $viet ;
}
save :b ;

Upvotes: 1

Related Questions