Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re^5: Strip XML document by poj
in thread Strip XML document by corfuitl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-26 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found