views:

284

answers:

1

Hi,

I'm working on an XML webservice (in PHP!), and in order to do things 'right', I want to use XMLWriter instead of just concatinating strings and hoping for the best.

I'm using XML namespaces everywhere using ->startElementNS and ->writeElementNS. The problem, is that every time I use these functions a new namespace declaration is also writting.

While this is correct syntax, it's a bit unneeded. I'd like to make sure my namespace declaration is only written the first time it's used in the context of a document.

Is there an easy way to go about this with XMLWriter, or am I stuck subclassing this and managing this manually.

Thanks, Evert

+1  A: 

You can pass NULL as the uri parameter.

<?php
$w = new XMLWriter;
$w->openMemory();
$w->setIndent(true);
$w->startElementNS('foo', 'bar', 'http://whatever/foo');
$w->startElementNS('foo', 'baz', null);
$w->endElement();
$w->endElement();
echo $w->outputMemory();
prints
<foo:bar xmlns:foo="http://whatever/foo"&gt;
 <foo:baz/>
</foo:bar>
VolkerK
Not exactly what I'm looking for. In your example, the 'bar' and 'baz' elements might be implemented by completely different objects and methods who are not always aware of each other..I __always__ want to specify the namespace, but I want it to only render the first time.
Evert
I haven't found any support for this in php/xmlwriter or libxml/xmlwriter. You probably have to keep track of this yourself.
VolkerK