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

Hi, how can i create an empty element like
<element />
with xml::smart? I did all the coding so far for an export interface with perl and xml::smart and this is the point where i am still failing. some elements need to be there, but empty, since i dont have values every time in every row... can you please help, me? thanks in advance, makro

Replies are listed 'Best First'.
Re: empty element with xml::smart
by ikegami (Patriarch) on Mar 18, 2008 at 06:06 UTC

    Can you do <element></element>? That's exactly the same thing as <element/>.

      Installed the module... It actually produces <element/> for empty elements:

      use XML::Smart qw( ); my $xml = XML::Smart->new(); $xml->{foo}{bar}{empty} = {}; print scalar $xml->data();
      <?xml version="1.0" encoding="iso-8859-1" ?> <?meta name="GENERATOR" content="XML::Smart/1.6.8 Perl/5.008008 [MSWin +32]" ?> <foo> <bar> <empty/> </bar> </foo>

      If you use a DTD, then you need to specify the "EMPTY" keyword or else it'll generate <empty></empty> (which is fine cause it means the same thing).

      use XML::Smart qw( ); my $xml = XML::Smart->new(); $xml->{foo}{bar}{empty} = {}; $xml->apply_dtd(<<'__EOI__'); <!DOCTYPE foo [ <!ELEMENT foo (bar*)> <!ELEMENT bar (empty*)> <!ELEMENT empty EMPTY> ]> __EOI__ print scalar $xml->data();
        thanks for the reply/solution. i was trying '' and undef as source values (from the db) for xml::smart, that didnt work the way i expected perl to work. sure, the empty hash, i should have tried that too. thanks for help!