Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: Strip XML document

by poj (Abbot)
on Dec 18, 2018 at 16:42 UTC ( #1227408=note: print w/replies, xml ) Need Help??

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

Can you have a TUV:property for more than one language ?. For example TUV:client:en and TUV:client:fr


Replies are listed 'Best First'.
Re^4: Strip XML document
by corfuitl (Sexton) on Dec 19, 2018 at 13:20 UTC

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

      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>
Re^4: Strip XML document
by corfuitl (Sexton) on Dec 19, 2018 at 18:18 UTC

    Fantastic!!! It works. Thank you so much!

Re^4: Strip XML document
by corfuitl (Sexton) on Dec 19, 2018 at 16:12 UTC

    Thanks for your efforts

    This works fine, however, how is it possible to export the formatting tags for the <seg>? I mean, the and other similar tags may appear.


      # ($props{$lang} = $tuv->findvalue('seg')) =~ s{\n}{<lb/>}g; my $seg = $tuv->findnodes('seg')->get_node(1); #first $props{$lang} = join '',map {$_->toString} $seg->childNodes(); $props{$lang} =~ s{\n}{<lb/>}g;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2021-04-16 09:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found