php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44108 SimpleXMLelement has no children() on element with only attributes?
Submitted: 2008-02-13 12:47 UTC Modified: 2008-02-13 17:51 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: php dot net at trueprices dot net Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.5 OS: Debian
Private report: No CVE-ID: None
 [2008-02-13 12:47 UTC] php dot net at trueprices dot net
Description:
------------
SimpleXMLelement has no children() on element with only attributes?

I try to retreive an element by xpath which goes without problems, The child elements it contains (same namespace) are all empty element with only attributes. However the returned SimpleXML element does not contain any children? so i'm unable to retreive there attributes.




Reproduce code:
---------------
<?php
$data = '<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions 
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
  targetNamespace="http://www.w3.org/2001/XMLSchema"
>
<wsdl:portType name="qxqPortType">
        <wsdl:operation name="SOAP_set">
            <wsdl:input message="SetRequest"/>
            <wsdl:output message="SetResponse"/>
        </wsdl:operation>
    <wsdl:operation name="SOAP_get">
            <wsdl:input message="GetRequest"/>
            <wsdl:output message="GetResponse"/>
        </wsdl:operation>        
</wsdl:portType>
</wsdl:definitions>';

echo '<pre>';
$xml = simplexml_load_string($data);
if ($xml)
{

  $res2 = $xml->xpath('//wsdl:portType/wsdl:operation');
  //print_r($res);
  foreach ($res2 as $child2)
  {
    //print_r($child2->children());
    echo 'child2'.PHP_EOL;
    print_r($child2->getName().PHP_EOL);
    print_r(count($child2->children()).PHP_EOL);
    print_r($child2);
    
    foreach ($child2->children() as $child3)
    {
      echo 'child3'.PHP_EOL;
      print_r($child3->getName().PHP_EOL);
      print_r(count($child3->children()).PHP_EOL);
      print_r($child3);
      foreach ($child3->children() as $child4)
      {
        echo 'child4'.PHP_EOL;
        print_r($child4->getName().PHP_EOL);
        print_r(count($child4->children()).PHP_EOL);
        print_r($child4);
      }
    }

  }
}
echo '</pre>';
?>

Expected result:
----------------
child2 & child3 & child4 should be printed..
 

child2
operation
2
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => SOAP_set
        )

)
child2
operation
2
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => SOAP_set
        )
   /* 2 another simplexml element input & output with children & attributes*/
)

Actual result:
--------------
Only child2 prints with 0 children

child2
operation
0
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => SOAP_set
        )

)
child2
operation
0
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => SOAP_get
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-13 16:22 UTC] hubert dot roksor at gmail dot com
If $child2 is <wsdl:operation/> then $child3 will be <wsdl:input/> or <wsdl:output/> and there will be no $child4.

Unless this behaviour is considered as a bug by the developer and until it is fixed, you'll have to specify the children's namespace when calling children():
- $child2->children('http://schemas.xmlsoap.org/wsdl/') on PHP 5.1 and later or
- $child2->children('wsdl', true) on PHP 5.2 and later
 [2008-02-13 17:51 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

xpath does not set the namespace scope of the returned SimpleXMLelement. 
You are required to set the namespace as demonstrated in previous 
comment
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Jun 07 00:03:41 2023 UTC