php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32307 asXML() doesn't work for empty XML elements
Submitted: 2005-03-15 06:20 UTC Modified: 2005-03-15 16:22 UTC
From: mike at cocoweb dot co dot uk Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.0.3 OS: Win XP
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mike at cocoweb dot co dot uk
New email:
PHP Version: OS:

 

 [2005-03-15 06:20 UTC] mike at cocoweb dot co dot uk
Description:
------------
asXML() function generates an XML string which contains invalid XML.


Reproduce code:
---------------
   // Valid XML
   $string =
   "
      <a>
         <b></b>
      </a>
   ";

   // Load the string into the simplexml framework
   $XMLObject = simplexml_load_string($string);

   // View the XML object in string representation.
   echo $XMLObject->asXML();

Expected result:
----------------
      <a>
         <b></b>
      </a>

Actual result:
--------------
      <a>
         <b/>
      </a>

<!-- 

Now the <b> tag is unclosed!  i.e. invalid XML.

I am probably doing something wrong... so please help :o)

I have checked the documentation and examples on other websites all to no avail.  Couldn't find anything relevant using Google.

-->


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-15 06:35 UTC] rasmus@php.net
That's not invalid XML.  <b/> is identical to <b></b> in XML and is perfectly valid.
 [2005-03-15 16:22 UTC] mike at cocoweb dot co dot uk
Thanks for the reply.  If it is valid XML then fair enough.

However I think asXML() function should be changed to be more useful.

I initially was running the code on something along the lines of:

"<textarea ...></textarea>" and asXML() was returning "<textarea .../>" which is not valid XHTML even though it's valid XML.  [See the latest XHTML specification reference at the end of this message].

I have XHTML embedded in my XML file so the asXML() function causes a mess.

I know this has nothing to do with XML, but asXML() would be more useful if it did not change the original XML text.  I think that asXML() should either NOT change the text, OR have a setting which either does or does not change the text.

e.g. asXML($xml_string, true) would output "<a />" whereas:

asXML($xml_string, false) could output "<a></a>".  It would be more useful then.

So even though the behaviour of asXML() is valid, I think it could be improved IMHO.

XHTML Specification Reference:

[C.3. Element Minimization and Empty Element Content

Given an empty instance of an element whose content model is not EMPTY (for example, an empty title or paragraph) do not use the minimized form (e.g. use <p> </p> and not <p />).]
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC