php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68756 Warn, when attributes added after subelements are ignored
Submitted: 2015-01-06 00:58 UTC Modified: 2016-08-11 17:40 UTC
From: bas5winkel at gmail dot com Assigned:
Status: Open Package: XML Writer
PHP Version: 5.5.20 OS: *
Private report: No CVE-ID: None
 [2015-01-06 00:58 UTC] bas5winkel at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.xmlwriter-write-attribute
---

If you intermix writing sub-elements and attributes, any attributes that are written after the first sub-element are ignored/discarded.


Test script:
---------------
<?php
$xml->startElement('element');
  $xml->writeAttribute('attr1', 0);
  $xml->writeElement('subelem', 0);
  $xml->writeAttribute('attr2', 0);
$xml->endElement();
?>


Expected result:
----------------
<element attr1=0 attr2=0>
  <subelem>0</subelem>
</element> 

Actual result:
--------------
<element attr1=0>
  <subelem>0</subelem>
</element> 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-06 01:25 UTC] requinix@php.net
-Package: XML Writer +Package: Documentation problem
 [2015-01-06 01:25 UTC] requinix@php.net
XMLWriter is a stream writer: once you've started writing a subelement you cannot "go back" and write an attribute.

Changing to a doc bug as I don't see this caveat mentioned anywhere.
 [2015-01-06 14:45 UTC] bas5winkel at gmail dot com
Thanks for the quick reply.
Would it be possible to thrown an exception when one tries write an attribute 
after a sub-element is written?
This would make it more obvious that one does something that is not possible.
It took me quite a while to figure out my mistake.
 [2016-08-11 17:40 UTC] cmb@php.net
-Summary: attributes added after subelements are ignored +Summary: Warn, when attributes added after subelements are ignored -Type: Bug +Type: Feature/Change Request -Package: Documentation problem +Package: XML Writer -Operating System: all? +Operating System: *
 [2016-08-11 17:40 UTC] cmb@php.net
> Changing to a doc bug as I don't see this caveat mentioned
> anywhere.

The introduction[1] already explains (emphasis mine):

| This extension represents a writer that provides a *non-cached*,
| *forward-only* means of generating streams or files containing
| XML data.

Anyhow, adding respective notes to each function's manual page
appears to be excessive. Consider, for instance, what would happen
when one calls ::startElement() directly after ::startComment().

> Would it be possible to thrown an exception when one tries write
> an attribute after a sub-element is written?

Well, an exception wouldn't fit with the procedural style (PHP
functions don't throw exceptions, yet), and would be quite
invasive wrt. BC anyway. A notice or a warning seem to be more
appropriate, if a call fails. Cf. <https://3v4l.org/9hpYR#v560>;
checking the return value of every call would really be tiresome.

[1] <http://php.net/manual/en/intro.xmlwriter.php>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC