Skip Menu |

This queue is for tickets about the XML-Simple CPAN distribution.

Report information
The Basics
Id: 7028
Status: resolved
Priority: 0/
Queue: XML-Simple

People
Owner: grantm [...] cpan.org
Requestors: jamesb-cpan [...] comcast.net
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 2.12
Fixed in: (no value)



Subject: SuppressEmpty with XMLout should skip undef values
SuppressEmpty with XMLout doesn't appear to change how undef values are output. They're always output as an empty tag. I have not found a way to change this behavior other than by removing undefs from the data to be output. Am I missing something? Am I dumb? A "yes" to either of those questions is perfectly fine by me. The documentation says: ---- The option also controls what XMLout() does with undefined values. Setting the option to undef causes undefined values to be output as empty elements (rather than empty attributes), it also suppresses the generation of warnings about undefined values. ---- However I'm unable to figure out how to make empty attributes from undef values. Test cases: ------------------------ #!/usr/bin/perl use strict; use XML::Simple; use Test::More qw(no_plan); use Data::Dumper; my $struct = { foo => '', bar => undef, }; # I'm guessing at what proper behavior should be. To me these values make # sense, but I'll take whatever works and is documented. # fails: my $case1 = XMLout($struct,SuppressEmpty => 1); ok($case1 =~ !m#<bar>#,"empty element for undef value shouldn't be created with SuppressEmpty => 1"); # fails: my $case2 = XMLout($struct,SuppressEmpty => ''); ok($case2 =~ !m#<bar>#,"empty element for undef value shouldn't be created with SuppressEmpty => ''"); # passes: my $case3 = XMLout($struct,SuppressEmpty => undef); ok($case3 =~ m#<bar>#,"empty element for undef value should be created SuppressEmpty => undef"); # passes: my $case4 = XMLout($struct); ok($case4 =~ m#<bar>#,"empty element for undef value should be created with no args");
The short answer is that XML::Simple has no facility for doing what you want to do - have undefined values skipped when generating XML. Originally, the SuppressEmpty tag only applied to XMLin() and supported three values: 1 empty tags skipped altogether when reading XML '' empty tags represented as empty strings undef empty tags represented as undefined values Later, support was added for XMLout() to recognise the option too. Where both XMLin() and XMLout() support an option, the aim is generally to have XMLout() undo whatever XMLin() did. With the last two option values ('' and undef) that's fairly straightforward. With the first option value (1) it's clearly impossible. Given that an option value of '1' has no meaning to XMLout(), an argument could be made that the value is 'spare' and the meaning you seek could be assigned to it. I will consider that possibility. The other issue is that the documentation does not clearly state that a value of '1' has no meaning for XMLout(). At the time I apparently thought it was obvious :-) If you want to send a patch then I'll consider it. Otherwise don't hold your breath. Regards Grant
The points raised have been addressed in the 2.13 release of XML::Simple uploaded today. The default behaviour of XMLout with regard to undef was broken. It should have output an empty attribute as per the documentation and behaviour of earlier releases. The code and tests have been fixed. The additional possibility of setting SuppressEmpty => 1 on output has also been implemented. This will cause undefined values to be skipped altogether. Thanks for your feedback.