in reply to prune xml of empty elements

Easiest way to do this is :
use Modern::Perl; my $str = qq@ <note> <to> <person>Satan</person> </to> <from via="postcard" russia="with love">moneypenny</from> <heading></heading> <body></body> </note>@; $str =~ s@<(.*?)></\1>@@g; say $str;
Output :
<note> <to> <person>Satan</person> </to> <from via="postcard" russia="with love">moneypenny</from> </note>
No modules or parsing needed, just a simple one line regex.

Replies are listed 'Best First'.
Re^2: prune xml of empty elements
by chromatic (Archbishop) on Jul 15, 2011 at 06:39 UTC
    No modules or parsing needed, just a simple one line regex.

    The only problem is that it doesn't prune every valid empty XML tag. If getting the correct result is important, use an XML parser.

Re^2: prune xml of empty elements
by happy.barney (Friar) on Jul 15, 2011 at 07:36 UTC
    $str =~ s@<(.*?)/>@@g; 1 while $str =~ s@<(.*?)>\s*</\1>@@g; $str =~ s@(?<=>)\s+(?=\n)@@g;
Re^2: prune xml of empty elements
by jdporter (Paladin) on Jul 15, 2011 at 02:46 UTC
    s@<(.*?)></\1>@@g;

    I know one guy who writes regexes like that.