Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a script that takes an xml template, and populates various fields. The order of the elements is important. Anyway, I've been using XML::Smart, and it works like a dream for the most part. The one thing I can't figure out is an easy way to remove empty elements. For example:

<?xml version="1.0" encoding="utf-8" ?> <?meta name="GENERATOR" content="XML::Smart/1.6.9 Perl/5.010000 [cygwi +n]" ?> <mgdToneUpdate xmlns="http://www.blah.net/telcel/mgdTone" xmlns:blah=" +http://www.blah.com/content" xmlns:jcr="http://www.jcp.org/jcr/1.0"> <blah:ppuid>blah602527000831</blah:ppuid> <thumbnailUpdate/> <prelistenUpdate> <prelistenMp3ResourceUpdate/> <prelistenWavResourceUpdate/> <prelistenSmafResourceUpdate/> <prelistenAmrwbResourceUpdate/> <prelistenRmfResourceUpdate/> </prelistenUpdate> <premiumUpdate> <mp3ResourceUpdate> <blah:resourceFilename/> <blah:resourceFilename>prelistenMTT00602527000831.mmf</blah:reso +urceFilename> <blah:deviceId/> <blah:deviceId>blackberry9000_ver1</blah:deviceId> <blah:deviceId>nokia_e71_ver1_subua2</blah:deviceId> <blah:deviceId>mot_l6_ver1</blah:deviceId> </mp3ResourceUpdate> <wavResourceUpdate/> <smafResourceUpdate/> <amrwbResourceUpdate/> <rmfResourceUpdate/> </premiumUpdate> </mgdToneUpdate>
I don't have a dtd file (not really sure how to make one), or if there is some other way to remove the empty elements (but otherwise maintain order of the xml).

Any tips appreciated!

Replies are listed 'Best First'.
Re: removing empty elements in xml?
by Your Mother (Archbishop) on Jan 29, 2010 at 18:12 UTC

    Maybe...

    use strict; use warnings; use XML::LibXML; my $doc = XML::LibXML->new->parse_fh(*DATA); for ( $doc->findnodes("//*") ) { $_->parentNode->removeChild($_) unless $_->textContent() =~ /\S/ or $_->hasAttributes(); } print $doc->serialize(1); __END__ <?xml version="1.0" encoding="utf-8" ?> <?meta name="GENERATOR" content="XML::Smart/1.6.9 Perl/5.010000 [cygwi +n]" ?> <mgdToneUpdate xmlns="http://www.blah.net/telcel/mgdTone" xmlns:blah=" +http://www.blah.com/content" xmlns:jcr="http://www.jcp.org/jcr/1.0"> <blah:ppuid>blah602527000831</blah:ppuid> <thumbnailUpdate/> <prelistenUpdate> <prelistenMp3ResourceUpdate/> <prelistenWavResourceUpdate/> <prelistenSmafResourceUpdate/> <prelistenAmrwbResourceUpdate/> <prelistenRmfResourceUpdate/> </prelistenUpdate> <premiumUpdate> <mp3ResourceUpdate> <blah:resourceFilename/> <blah:resourceFilename>prelistenMTT00602527000831.mmf</blah:reso +urceFilename> <blah:deviceId/> <blah:deviceId>blackberry9000_ver1</blah:deviceId> <blah:deviceId>nokia_e71_ver1_subua2</blah:deviceId> <blah:deviceId>mot_l6_ver1</blah:deviceId> </mp3ResourceUpdate> <wavResourceUpdate/> <smafResourceUpdate/> <amrwbResourceUpdate/> <rmfResourceUpdate/> </premiumUpdate> </mgdToneUpdate>

    Gives (some whitespace snipped)-

    <?xml version="1.0" encoding="utf-8"?> <?meta name="GENERATOR" content="XML::Smart/1.6.9 Perl/5.010000 [cygwi +n]" ?> <mgdToneUpdate xmlns="http://www.blah.net/telcel/mgdTone" xmlns:blah=" +http://www.blah.com/content" xmlns:jcr="http://www.jcp.org/jcr/1.0"> <blah:ppuid>blah602527000831</blah:ppuid> <premiumUpdate> <mp3ResourceUpdate> <blah:resourceFilename>prelistenMTT00602527000831.mmf</blah:reso +urceFilename> <blah:deviceId>blackberry9000_ver1</blah:deviceId> <blah:deviceId>nokia_e71_ver1_subua2</blah:deviceId> <blah:deviceId>mot_l6_ver1</blah:deviceId> </mp3ResourceUpdate> </premiumUpdate> </mgdToneUpdate>