php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #65172 SimpleXMLElement::children() incorrect behavior
Submitted: 2013-07-01 07:54 UTC Modified: 2014-04-03 11:05 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: scruoge at gmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 5.3.26 OS: Windows & Linux
Private report: No CVE-ID: None
 [2013-07-01 07:54 UTC] scruoge at gmail dot com
Description:
------------
XPath, selecting attribute returns SimpleXMLElement, which's children() method returns NULL.

Probably it's a documentation issue. It says

"Returns a SimpleXMLElement element, whether the node has children or not."


Test script:
---------------
<?php
$s = '<rootNode><childNode attr="attrval" attr2="attr2val">nodeContents</childNode></rootNode>';
$xml = new SimpleXMLElement($s);
$xpathres = $xml->xpath('childNode/@attr');
$xres = array_shift($xpathres);
$xres = array_shift($xml->xpath('childNode/@attr'));
echo '$xres->children() result: ';
var_dump($xres->children());
echo 'typecast result: ';
var_dump((string) $xres);


Expected result:
----------------
$xres->children() result: object(SimpleXMLElement)#2 (0) {
}
typecast result: string(7) "attrval"

Actual result:
--------------
$xres->children() result: NULL
typecast result: string(7) "attrval"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-01 07:58 UTC] scruoge at gmail dot com
There's extra string in the code that should be removed:
(line 6) $xres = array_shift($xml->xpath('childNode/@attr'));

It does not affect test script result, though it generates E_STRICT notice.
 [2013-07-01 09:27 UTC] scruoge at gmail dot com
-Operating System: Windows +Operating System: Windows & Linux
 [2013-07-01 09:27 UTC] scruoge at gmail dot com
The bug is also reproducible in Linux environment.
 [2013-07-19 19:07 UTC] mail+php at requinix dot net
...Attributes can't have children.

If you mean the difference between returning an empty SimpleXMLElement object 
versus returning null, I can see that as a doc bug: Return Values for children() 
should say
"Returns NULL if called on a SimpleXMLElement object that represents an attribute 
and not a tag."
as it does for attributes().
 [2013-07-22 08:37 UTC] scruoge at gmail dot com
Yes, I agree.
As I suspected it's a documentation problem.
 [2014-04-03 11:05 UTC] ab@php.net
-Type: Bug +Type: Documentation Problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC