php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #56239 count for SimpleXml nodes return wrong value
Submitted: 2004-11-29 23:46 UTC Modified: 2013-07-31 05:22 UTC
From: tim dot lokot at printsoft dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5CVS-2004-11-29 (dev) OS: XP
Private report: No CVE-ID: None
 [2004-11-29 23:46 UTC] tim dot lokot at printsoft dot com
Description:
------------
PHP is 5.0.2 firstly, it wasn't in the list, so I just chose the cvs version.

I am trying to count how many nodes are under another node in an xml document by counting them with count().  For some reason, even when I have more than one node, it always returns 1.  Same with the children() function.

Basically in my code, I'm trying to prove that Order->OrderLines['NumLines'] == count(Order->OrderLines->OrderLine)

Reproduce code:
---------------
XML Fragment:

  <Order>
    <OrderLines NumLines="2">
      <OrderLine Number="1" StampID="435" Quantity="2" />
      <OrderLine Number="2" StampID="733" Quantity="5" />
    </OrderLines>
  </Order>


PHP Code;

$order = $xml->Order; // set previously

  count($order->OrderLines->children()))

    or

  count($order->OrderLines->OrderLine)

Expected result:
----------------
2

Actual result:
--------------
1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-31 05:22 UTC] yohgaki@php.net
-Status: Open +Status: Not a bug -Package: domxml +Package: *General Issues
 [2013-07-31 05:22 UTC] yohgaki@php.net
Usage is wrong. Please read 
http://jp1.php.net/manual/en/book.simplexml.php

In simplexml, root node is assumed always exists and represented as var. So you 
cannot refer to it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 22:01:33 2024 UTC