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

Re^5: Strip XML document

by poj (Abbot)
on Dec 19, 2018 at 13:40 UTC ( [id://1227465]=note: print w/replies, xml ) Need Help??


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

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>
poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found