ドキュメント内の目的の場所に名前空間を1回書き出すことができます。これは、一番上の要素のfeです。
$writer = new XMLWriter();
$writer->openURI('php://output');
$writer->startDocument('1.0');
$writer->startElement('sample');
$writer->writeAttributeNS('xmlns','foo', null,'http://foo.org/ns/foo#');
$writer->writeAttributeNS('xmlns','bar', null, 'http://foo.org/ns/bar#');
$writer->writeElementNS('foo','quz', null,'stuff here');
$writer->writeElementNS('bar','quz', null,'stuff there');
$writer->endElement();
$writer->endDocument();
$writer->flush(true);
これは次のようなものになるはずです
<?xml version="1.0"?>
<sample xmlns:foo="http://foo.org/ns/foo#" xmlns:bar="http://foo.org/ns/bar#">
<foo:quz>stuff here</foo:quz>
<bar:quz>stuff there</bar:quz>
</sample>
これは一種の厄介なxmlwriterであり、これらの宣言を追跡しません。無効なxmlを書き込むことができます。また、属性がnullになる可能性がある場合でも、属性が必要であり、最後ではなく3番目の引数であることが煩わしいです。
$ 2c、*-パイク