Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^4: Strip XML document

by corfuitl (Sexton)
on Dec 19, 2018 at 13:20 UTC ( #1227462=note: print w/replies, xml ) Need Help??

in reply to Re^3: Strip XML document
in thread Strip XML document

Oh, yes! It should be for each language because both languages may have prop

Replies are listed 'Best First'.
Re^5: Strip XML document
by poj (Abbot) on Dec 19, 2018 at 13:40 UTC

    Ok, try this

    #!/usr/bin/perl use strict; use XML::LibXML; use List::Util qw{ uniq }; my $dom = 'XML::LibXML'->load_xml(IO => *DATA); my @headers = (); my @lang = (); for ($dom->findnodes('//tu/prop/@type')){ push @headers, 'tu:'.$_->value; } for my $tuv ($dom->findnodes('//tu/tuv')){ my $lang = $tuv->findvalue('@xml:lang'); push @lang, $lang; for ($tuv->findnodes('prop/@type')){ push @headers, 'tuv:'.$_->value.':'.$lang; } } @headers = sort +uniq(@headers); @lang = sort +uniq(@lang); push @headers,@lang; # add lang open OUT,'>','report.dat' or die "$!"; print OUT join "\t",@headers,"\n"; for my $tu ($dom->findnodes('/tmx/body/tu')) { my %props; for my $prop ($tu->findnodes('prop')) { $props{ 'tu:'.$prop->findvalue('@type') } = $prop->textContent; } for my $tuv ($tu->findnodes('tuv')) { my $lang = $tuv->findvalue('@xml:lang'); ($props{$lang} = $tuv->findvalue('seg')) =~ s{\n}{<lb/>}g; for my $prop ($tuv->findnodes('prop')) { my $name = join ':','tuv',$prop->findvalue('@type'),$lang; $props{$name} = $prop->textContent; } } print OUT join ("\t", map $_ // "", @props{@headers}), "\t"; print OUT "\n"; } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <tmx version="1.4"> <header creationtool="xx" creationtoolversion="1" segtype="sentence" o +-tmf="undefined" adminlang="en" srclang="en" datatype="undefined"> </header> <body> <tu changedate="20180321T113135Z" creationdate="20180321T113135Z" chan +geid="user" tuid="1"> <prop type="client"> a </prop> <prop type="project"> b </prop> <prop type="domain"> c </prop> <prop type="subject"> d </prop> <prop type="corrected">no</prop> <prop type="aligned">no</prop> <tuv xml:lang="en"><seg>Hello <b>world!</b></seg></tuv> <tuv xml:lang="fr"><seg>Bonjour <b> monde</b></seg></tuv> </tu> <tu changedate="20180321T113135Z" creationdate="20180321T113135Z" chan +geid="user2" tuid="2"> <prop type="client"> 1 </prop> <prop type="project">yes</prop> <prop type="corrected">no</prop> <prop type="aligned">no</prop> <tuv xml:lang="en"> <prop type="client"> zyx </prop> <seg>Hello <b>world!</b></seg> </tuv> <tuv xml:lang="fr"> <prop type="client"> 2 </prop> <prop type="project">yes 2</prop> <prop type="corrected">no 2</prop> <prop type="aligned">no 2</prop> <seg>Bonjour <b> monde</b></seg> </tuv> </tu> </body> </tmx>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1227462]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2021-05-08 22:23 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (98 votes). Check out past polls.