我有以下巨大的 XML 文件:
<?xml version="1.0" encoding="utf-16"?>
<!DOCTYPE tmx SYSTEM "56.dtd">
<body>
<tu changedate="20130625T175037Z"">
<tuv xml:lang="pt-pt">
<prop type="x-context-pre"><seg>Some text.</seg></prop>
<prop type="x-context-post"><seg>Other text.</seg></prop>
<seg>The text I'm interested.</seg>
</tuv>
<tuv xml:lang="it">
<seg>And it's translation in italian.</seg>
</tuv>
</tu>
.... followed by other <tu>'s
</body>
我正在使用哈希将每个“seg”内容与其更改日期“关联”,以便我可以使用处理程序检查“seg”是否已经存在,然后查看找到的版本是否较旧,如果是,删除它。这种方法的问题是,如果找到的版本较新,则无法删除在 xml 文件中解析的旧版本。这是我到目前为止的代码:
use 5.010;
use strict;
use warnings;
use XML::Twig;
use Digest::MD5 qw(md5);
my $filename = 'pt_PT-it_IT.tmx';
my $out_filename = 'out.xml';
open my $out, '>', $out_filename;
binmode $out;
my $original_twig = new XML::Twig (pretty_print => 'indented', twig_handlers => {tu => \&original_tu});
$original_twig->parsefile($filename);
$original_twig->flush($out);
close $out;
{ my %md5;
sub original_tu {
my($twig, $original_tu) = @_;
#print $original_tu->text;
my $original_seg = $original_tu->first_child('tuv')->first_child('seg')->text;
my $original_changedate = $original_tu->att('changedate');
$original_changedate = substr $original_changedate, 0, 8;
$hash = md5(original_seg);
if (exists($md5{$hash})) {
if (($md5{$hash}) gt $original_changedate) {
print "================================\n";
print "DELETED\n";
print $original_seg;
print "\n BECAUSE ORIGINAL DATE: ";
print $original_changedate;
print " IS OLDER THAN THE FOUND ONE: ";
print $other_changedate;
print "\n=================================\n";
$original_tu->delete();
}
}
else
$md5{$hash} = $original_changedate;
}
}
提前感谢任何关于如何(概括)删除巨大(700 MB)XML文件中具有最新值'changedate'的重复项的任何提示。
谢谢!