http://www.perlmonks.org?node_id=1008638


in reply to Filter for XML elements

Step 1 is: drop XML::Simple like a rock. If you continue along that route you're in for a world of pain. XML::Simple does have its uses, but what you're doing is not one of them.

XML::Twig (mentioned above) is specifically designed for the sort of task you're asking about - setting up a bunch of rules to handle incoming XML features and then streaming some XML through them.

Personally I'd generally use XML::LibXML - not because it's better than XML::Twig for this particular task but because it's the XML library I'm most familiar with. Here's how you could achieve your desired output using XML::LibXML...

use XML::LibXML 2; use XML::LibXML::PrettyPrint 'print_xml'; my $xml = XML::LibXML->load_xml(IO => \*DATA); # Promote <arit> elements out of their <z> container $xml -> findnodes('//z/arit') -> foreach(sub { $_->parentNode->parentNode->appendChild($_) }); # Remove certain elements $xml -> findnodes('//z | //runhd | //i-g | //zp_pvg | //zp_cl | //ar | +//zp_gr') -> foreach(sub { $_->parentNode->removeChild($_) }); print_xml $xml; __DATA__ <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 tranID="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 Symbol</z><syn>Something </syn><cf>(positiv +e)</cf> </obj-g><zp_gr /><z>some symbol</z><gr-g><gr gr="P" /><z><arit>1+1=3</ +arit> </z></gr-g></pv-g></entry>
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'