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

Hello Monks,

I am building a data structure for outputting an xml document that consists exclusively of nested elements EXCEPT for xmlns attributes. I would like to output XML with no attributes EXCEPT for xmlns attributes to the root element.
With noattr=1 the entire document looks great except for the xmlns entries.
With noattr=0 the root element looks great but the rest of the document consists of everything being attributes.
As you can imagine, this is a bit frustrating. Is there a solution to this? Or do I have to learn a new XML module? Thanks in advance for any help you can provide.

Oz

Replies are listed 'Best First'.
Re: XML::Simple and xmlns attributes
by psini (Deacon) on Jun 10, 2008 at 10:48 UTC

    If you are building the document from scratch (ie not from a data structure but as a process output) I strongly suggest XML::Writer.

    If you really want to use XML::Simple, read the POD's part relative to options, and specifically KeyAttr and ForceArray options. To obtain your document you should force array folding and generate your data structure so that any node not containing other nodes is an array

    Careful with that hash Eugene.

      Thank you for the reply...
      In this case I'm generating XML that will be sent to a web service using SOAP::Lite versus outputting a file to disk. The XML is generated from data read in from a delimited text file (not columnar csv though).
      ForceArray=1 or 0 has no effect on the output when I toggle it. I'll keep it on and try modifying KeyAttr once again. I assume that every nested element key that gets output as an attribute should be listed as a KeyAttr so that it will be output as an element?

        I think that, to do that with XML::Simple, you should rethink the data structure that you pass to XMLout.

        Try and read XML::Writer POD and you'll see that it is so much easier for your needs...

        Careful with that hash Eugene.

Re: XML::Simple and xmlns attributes
by Jenda (Abbot) on Jun 12, 2008 at 00:34 UTC

    I bet the root tag and its attributes are static, right? Use XML::Simple to create the stuff that's supposed to be inside the root tag and prepend the <root xmlns:nonsense="..."> and append the </root>. If there are supposed to be more tags inside the root one, generate the XML with a bogus root tag and then remove the root tag from each side of the generated string. I think this should require the least amount of changes to your code.