Dear all,

I am new to Perl / XML::Simple and my only programming experience was with Pascal about 10 years ago.

However there is something I really want to do when I handle some xml files. The code I am dealing with is something like this:

<entry rootUID="281088" Class="A" Name="abc"><h-g><h>abc</h><runhd>abc +</runhd><z>/</z><i-g><i>abc123</i></i-g><z>/</z></h-g><zp_pvg /><pv-g +><pv>abc is 123</pv><z> (</z><r r="REG" /><z><it2>Reg</it2>) </z><d t +ranID="1" status="3">abc is a company.<chn localeUID="202" status="3" +>(Chinese translation)</chn></d><z>: </z><x tranID="2" status="3">abc + launched in 1992.<chn localeUID="202" status="3">Some other Chinese +translation</chn></x><obj-g><zp_cl /><z>Symbolmark</z><cl>rules</cl>< +ar>, </ar><cl>decision</cl><ar>, </ar><cl>the law</cl><z>Another Symb +ol</z><syn>Something </syn><cf>(positive)</cf></obj-g><zp_gr /><z>som +e symbol</z><gr-g><gr gr="P" /><z><arit>1+1=3</arit></z></gr-g></pv-g +></entry>

What I want to do is to extract only the things I need from the code above, and generate something like below:

<entry rootUID="281088" Class="A" Name="abc"> <h-g> <h>abc</h> <pv-g> <pv>abc is 123</pv> <r r="REG" /> <d tranID="1" status="3">abc is a company. <chn localeUID="1" status="3">(Chinese translation)</chn> </d> <x tranID="2" status="3">abc launched in 1992. <chn localeUID="202" status="3">Some other Chinese translation</chn> </x> <obj-g> <cl>rules</cl> <cl>decision</cl> <cl>the law</cl> <syn>Something </syn><cf>(positive)</cf> </obj-g> <gr-g> <gr gr="P" /> <arit>1+1=3</arit> </gr-g> </pv-g> </entry>

I successfully converted the xml data into this:

$VAR1 = { 'entry' => { 'h-g' => { 'runhd' => 'abc', 'h' => 'abc', 'z' => [ '/', '/' ], 'i-g' => { 'i' => 'abc123' } }, 'rootUID' => '281088', 'zp_pvg' => {}, 'Class' => 'A', 'Name' => 'abc', 'pv-g' => { 'zp_gr' => {}, 'r' => { 'r' => 'REG' }, 'pv' => 'abc is 123', 'gr-g' => { 'gr' => { 'gr' => 'P' }, 'z' => { 'arit' => '1+1=3' } }, 'x' => { 'chn' => { 'localeUID' => '202', 'status' => '3', 'content' => 'Some othe +r Chinese translation' }, 'tranID' => '2', 'status' => '3', 'content' => 'abc launched in 19 +92.' }, 'd' => { 'chn' => { 'localeUID' => '202', 'status' => '3', 'content' => '(Chinese +translatio n)' }, 'tranID' => '1', 'status' => '3', 'content' => 'abc is a company.' }, 'obj-g' => { 'cl' => [ 'rules', 'decision', 'the law' ], 'cf' => '(positive)', 'syn' => 'Something ', 'z' => [ 'Symbolmark', 'Another Symbol' ], 'zp_cl' => {}, 'ar' => [ ', ', ', ' ] }, 'z' => [ ' (', { 'it2' => 'Reg', 'content' => ') ' }, ': ', 'some symbol' ] } } };

but I don't know how to add filter to the file

In my mind, first, I need to use Simple::XML XMLin to open the file, so to convert the XML into perl structure

second, I need to set some if, then rules to preserve the data

Lastly, to wrap the data back into XML using Simple:xml.

But after some reading, I still have no idea on what to do with Step 2 and Step 3...

Can anyne help?


In reply to Filter for XML elements by uituit

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.