Dear Perlmonks,

I'm trying to convert an xml file i have into a more readable csv file. it contains things like this:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE xliff PUBLIC "-//XLIFF//DTD XLIFF//EN" "http://www.oasis-ope +n.org/committees/xliff/documents/xliff.dtd" > <xliff version="1.0"> <file datatype="plaintext" original="game/stringtable/Example.stt" so +urce-language="en" target-language="de" xml:space="default"> <header/> <body> <trans-unit approved="yes" id="Example_monsternpc_greeting" reforma +t="yes" translate="yes" xml:space="default"> <source>'I am a monster NPC.'</source><target state="translated" x +ml:lang="de">'Ich bin ein Monster-NSC.'</target> <prop-group><prop prop-type="entryid">1173668791078</prop></prop-gr +oup><alt-trans match-quality="100" origin="5k" xml:space="default"><s +ource xml:lang="en">'I am a monster NPC.'</source><target xml:lang="d +e">'Ich bin ein Monster-NSC.'</target><prop-group><prop prop-type="en +tryid">1173668807656</prop></prop-group></alt-trans></trans-unit> <trans-unit approved="yes" id="Example_monsternpc_pc_transform" ref +ormat="yes" translate="yes" xml:space="default"> <source>'Turn into a monster.'</source><target xml:lang="de">'In e +in Monster verwandeln.'</target> <prop-group><prop prop-type="entryid">1173668791093</prop></prop-gr +oup><alt-trans match-quality="100" origin="5k" xml:space="default"><s +ource xml:lang="en">'Turn into a monster.'</source><target xml:lang=" +de">'In ein Monster verwandeln.'</target><prop-group><prop prop-type= +"entryid">1173668807671</prop></prop-group></alt-trans></trans-unit> <trans-unit approved="yes" id="Example_private_testMe2" reformat="y +es" translate="yes" xml:space="default"> <source>and again</source><target xml:lang="de">und noch einer</ta +rget> <prop-group><prop prop-type="entryid">1173668791109</prop></prop-gr +oup><alt-trans match-quality="100" origin="5k" xml:space="default"><s +ource xml:lang="en">and again</source><target xml:lang="de">und noch +einer</target><prop-group><prop prop-type="entryid">1173668807687</pr +op></prop-group></alt-trans></trans-unit> </body> </file> <file datatype="plaintext" original="game/stringtable/training_skelet +on.stt" source-language="en" target-language="de" xml:space="default" +> <header/> <body> <trans-unit approved="yes" id="training_skeleton_ID_KillMe" reforma +t="yes" translate="yes" xml:space="default"> <source>You should get to know Sigmund before leaving to explore S +tormreach.</source><target xml:lang="de">Ihr solltet Sigmund kennenle +rnen, bevor Ihr Euch aufmacht, um Stormreach zu erkunden.</target> <prop-group><prop prop-type="entryid">1173668791328</prop></prop-gr +oup><alt-trans match-quality="100" origin="5k" xml:space="default"><s +ource xml:lang="en">You should get to know Sigmund before leaving to +explore Stormreach.</source><target xml:lang="de">Ihr solltet Sigmund + kennenlernen, bevor Ihr Euch aufmacht, um Stormreach zu erkunden.</t +arget><prop-group><prop prop-type="entryid">1173668807906</prop></pro +p-group></alt-trans></trans-unit> </body> </file> </xliff>
My script to convert it looks as follows:
#!/bin/perl use strict; use warnings; use XML::Simple; my $config = XMLin("meep.xlf", ForceArray => 1); open(EXTR, ">meep.csv") or die $!; foreach my $i ( 0 .. $#{ $config->{file} } ) { my $file = $config->{file}->[$i]->{original}; $file =~ s/game\/stringtable\///i; for my $entry ( keys %{ $config->{file}->[$i]->{body}->[0]->{'tran +s-unit'} } ) { my $source = $config->{file}->[$i]->{body}->[0]->{'trans-unit' +}->{$entry}->{source}->[0]; my $target = $config->{file}->[$i]->{body}->[0]->{'trans-unit' +}->{$entry}->{target}->[0]->{content}; print EXTR '"'.$file.'","'.$entry.'","'.$source.'","'.$target. +'"'."\n"; } } close EXTR;
And the output is this:
"Example.stt","Example_monsternpc_greeting","'I am a monster NPC.'","' +Ich bin ein Monster-NSC.'" "Example.stt","Example_private_testMe2","and again","und noch einer" "Example.stt","Example_monsternpc_pc_transform","'Turn into a monster. +'","'In ein Monster verwandeln.'" "training_skeleton.stt","training_skeleton_ID_KillMe","You should get +to know Sigmund before leaving to explore Stormreach.","Ihr solltet S +igmund kennenlernen, bevor Ihr Euch aufmacht, um Stormreach zu erkund +en."
The problem i have with this is: For some reason it has decided to change the order in which the elements are in the original file. (And it apprently does so randomly.) Is there a way to tell XMLIn something to the effect of: Just leave that stuff in the order you found it!

Or have i done something fundamentally wrong? :/

Greetings, Xenofur

In reply to sort order of imported xml data? by Xenofur

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.