php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74491 XMLWriter adds to many namespace definitions
Submitted: 2017-04-23 10:44 UTC Modified: 2018-04-02 22:51 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: subjective@php.net Assigned:
Status: Verified Package: XML Writer
PHP Version: 7.1.4 OS:
Private report: No CVE-ID: None
 [2017-04-23 10:44 UTC] subjective@php.net
Description:
------------
XMLWriter adds namespace definitions to any element node that is created by by a namespace aware method. It does not take definitions on parent nodes into account. The resulting XML output is valid and has even the same meaning, but it is a lot larger then needed.  



Test script:
---------------
<?php
$_ = new XMLWriter();
$_->openMemory();
$_->setIndent(2);

$_->startDocument();
$_->startElementNS('a', 'foo', 'urn:a');
$_->writeElementNS('a', 'bar', 'urn:a', 'foobar');
$_->endElement();
$_->endDocument();

echo $_->outputMemory();

Expected result:
----------------
<?xml version="1.0"?>
<a:foo xmlns:a="urn:a">
  <a:bar>foobar</a:bar>
</a:foo>

Actual result:
--------------
<?xml version="1.0"?>
<a:foo xmlns:a="urn:a">
  <a:bar xmlns:a="urn:a">foobar</a:bar>
</a:foo>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-06-28 18:09 UTC] paul at sparrowhawkcomputing dot com
I very much agree that this should be fixed; altho I suspect the problem is actually with xmlwrite.c in libxml, hence it's probably not be up to PHP to fix it :-(  But maybe the PHP powers that be can put some pressure on libxml to fix it.

Until it does get fixed, there is a fairly simple workaround:

If you know there is an in-scope namespace decl aready written (i.e., on an ancestor element) then simply pass null as the namespaceURI argument to XMLWriter::writeElementNS() and kin.
 [2018-04-02 22:51 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2018-04-02 22:51 UTC] cmb@php.net
> […] altho I suspect the problem is actually with xmlwrite.c in
> libxml, hence it's probably not be up to PHP to fix it […]

That!

> […] simply pass null as the namespaceURI argument to
> XMLWriter::writeElementNS() and kin.

Dead-on, again!  Thanks!  Hence, I'm changing to documentation
issue.  (By the way, the $prefix can also be NULL, to omit the
namespace).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 08:01:28 2024 UTC