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

I'm having problems producing an XML document with XML::Writer. the document looks something like...
<ROOT-NS:doctag xmlns:ROOT-NS="http://www.xxxx.com/yyy">
  <ROOT-NS:bodytag>
     <data xmlns="http://www.xxxx.com/zzz">
         <test>ok</test>
     </data>
  </ROOT-NS:bodytag>
</ROOT-NS:doctag>
Notice the data tag.
It has a namespace attribute with no name.
The Java guys I'm working with say that this is perfectly legal and it is called a default namespace.
When I try and use XML::Writer to do the same thing I always get __NS1 when I don't set a namespace name in my prefix map.

here is my prefix map...
$root_ns = "http://www.xxxx.com/yyy"; $data_ns = "http://www.xxxx.com/zzz"; $prefix_map = { $root_ns => 'ROOT-NS', $data_ns => '' };
This makes my data tag look like...
<__NS1:data xmlns:__NS1="http://www.xxxx.com/zzz">
    <test>ok<test>
<__NS1:data>

My XML document object is created with...
$xml = XML::Writer->new(DATA_MODE => 1, DATA_INDENT => 2, NAMESPACES = +> 1, PREFIX_MAP => $prefix_map);
... and the data tag section looks like...
$xml->startTag([[$data_ns, 'data']]); $xml->dataElement('test', 'ok'); $xml->endTag();
The perldocs for XML::Writer say...

"
The keys in the hash table are namespace URIs, and the values are the associated prefixes. If there is not a preferred prefix for the namespace URI in this hash, then the module will automatically generate prefixes of the form "__NS1", "__NS2", etc.

To set the default namespace, use '' for the prefix.
"

What am I doing wrong?

Replies are listed 'Best First'.
Re: XML::Writer and the default namespace
by diotalevi (Canon) on Feb 27, 2003 at 03:23 UTC

    It sounds like you should either alter your local XML::Writer install or use a different module. But really - it doesn't matter whether your XML is character identical with the Java folk's XML. The whole point here is that the namespace is useful for distinguishing between which namespace an element belongs to. Any namespace respecting XML parser should be able to handle the data in either mode and not give you issues. This is jsut basic XML.

    So I don't think its a problem.


    Seeking Green geeks in Minnesota

      I don't disagree with you but my Java guys are still having a cow over the issue. I'm hoping there is something I'm not setting up quite right in my XML::Writer calls that would solve this.

        And I would have figured the Java people would be all over XML standards. Or perhaps they aren't using a real parser and it doesn't work if you give it differently formatted data. Anyway, I think you're going to just end up either subclassing or forking XML::Writer. I'll bet its just a few lines that need changing - chek it out.


        Seeking Green geeks in Minnesota