php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65569 ChildNodes of DOMNodes list great childs, etc. as DOMText
Submitted: 2013-08-28 08:45 UTC Modified: 2015-07-08 21:23 UTC
From: Laurent dot Lyaudet at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: DOM XML related
PHP Version: 5.4.19 OS: Debian Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: Laurent dot Lyaudet at gmail dot com
New email:
PHP Version: OS:

 

 [2013-08-28 08:45 UTC] Laurent dot Lyaudet at gmail dot com
Description:
------------
Hi,

When using DOMNodes and other heriting classes (DOMDocument, DOMElement, etc.),
the childNodes attribute lists real childrens as DOMElement mixed with DOMText for the content of grandchildren.

It is similar to bug 40175 but more detailed. Moreover I don't see why the manual is cited as a justification for bug 40175:
from http://www.php.net/manual/en/class.domnode.php :
> childNodes
>
>    A DOMNodeList that contains all children of this node. If there are no >children, this is an empty DOMNodeList.


I join a debug function to supplement the bug in var_dump (see bug 65565 : https://bugs.php.net/bug.php?id=65565&thanks=4).

function debugSousBalises($p_oBalise){

  for($i = 0; $i < $p_oBalise->childNodes->length; ++$i){
    $oNode = $p_oBalise->childNodes->item($i);
    //var_dump($oNode);
    switch(get_class($oNode)){
      case 'DOMText':
        echo $oNode->wholeText;
        break;
      case 'DOMElement':
        echo $oNode->tagName.' : '.$oNode->nodeValue;
        break;
      default:
          echo get_class($oNode).' pas encore pris en compte';
    }
  }

}

used on the following fragment of xml 
<balise>
  <ata>2013-08-20T17:13:25</ata>
  <atd>2013-08-20T17:15:03</atd>
  <callSequence>1</callSequence>
  <actualCallSequence>2</actualCallSequence>
  <clauseInfo>
    <clause>LIV_CFM</clause>
    <description>Conforme</description>
  </clauseInfo>
</balise>

it will yield:
  ata : 2013-08-20T17:13:25
  atd : 2013-08-20T17:15:03
  callSequence : 1
  actualCallSequence : 2
  clauseInfo :
    LIV_CFM
    Conforme

Best regards,
   Laurent Lyaudet



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-08-28 08:55 UTC] Laurent dot Lyaudet at gmail dot com
My bad, it looks similar to bug 40175 but looking at the sample code for 40175 it is totally different. 40175 use getElementsByTagName but the description says 
The childNodes "function" returns a DOMNodeList (I added the quotes)
I mentally corrected to "The childNodes is a DOMNodeList" when I first looked at this shit.
 [2013-12-02 16:24 UTC] mike@php.net
-Status: Open +Status: Feedback
 [2013-12-02 16:24 UTC] mike@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.



 [2014-02-23 11:44 UTC] Laurent dot Lyaudet at gmail dot com
-Status: Feedback +Status: Open
 [2014-02-23 11:44 UTC] Laurent dot Lyaudet at gmail dot com
Hi,
I'm not sure what information is missing from the bug description but here are more explanations :
- an XML document is mainly a syntactical formulation of a rooted labeled tree structure.
- in a rooted tree structure, "A is the parent of B" means that A is adjacent (linked by an edge) to B and A is on the path between B and the root of the tree (B may be the root of the tree). If "A is the parent of B", "B is a child of A".
Let R be the label of the root, consider the following example:
      R
     / \
    A1  A2
   / \
  B1  B2
Nodes with labels A1 and A2 are "children" of node with label R.
Nodes with labels B1 and B2 are "grandchildren" of node with label R.
This labeled tree can be represented by the following XML :
<R>
 <A1>
  <B1/>
  <B2/>
 </A1>
 <A2/>
</R>

Assume R is a DOMNode,
expected content of childNodes of R :
A1, A2 as DOMElements
actual bugged content of childNodes of R :
A1 as DOMElement, text content of B1 as DOMText, 
text content of B2 as DOMText, A2 as DOMElement

Best regards,
   Laurent Lyaudet
 [2014-12-12 09:09 UTC] maus at hab dot de
In your example the nodes B1 and B2 do not have text content. I cannot reproduce this bug:

php --version
PHP 5.5.16 (cli) (built: Aug 21 2014 14:25:20) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

<?php

$xml = new DOMDocument();
$xml->loadXml('<R><A1><B1>B1</B1><B2>B2</B2></A1><A2/></R>');

foreach ($xml->documentElement->childNodes as $childnode) {
    echo get_class($childnode) . "\n";
}

Gives:

DOMElement
DOMElement

Where is the bug in your first example?
 [2014-12-13 10:24 UTC] Laurent dot Lyaudet at gmail dot com
Hi,
the bug appears with this test script :

<?php
function debugSousBalises($p_oBalise){

  for($i = 0; $i < $p_oBalise->childNodes->length; ++$i){
    $oNode = $p_oBalise->childNodes->item($i);
    //var_dump($oNode);
    switch(get_class($oNode)){
      case 'DOMText':
        echo $oNode->wholeText;
        break;
      case 'DOMElement':
        echo $oNode->tagName.' : '.$oNode->nodeValue;
        break;
      default:
          echo get_class($oNode).' pas encore pris en compte';
    }
  }

}


$xml = new DOMDocument();
$xml->loadXml('<balise>
  <ata>2013-08-20T17:13:25</ata>
  <atd>2013-08-20T17:15:03</atd>
  <callSequence>1</callSequence>
  <actualCallSequence>2</actualCallSequence>
  <clauseInfo>
    <clause>LIV_CFM</clause>
    <description>Conforme</description>
  </clauseInfo>
</balise>');


debugSousBalises($xml->documentElement);
?>

It gives 

  ata : 2013-08-20T17:13:25
  atd : 2013-08-20T17:15:03
  callSequence : 1
  actualCallSequence : 2
  clauseInfo : 
    LIV_CFM
    Conforme

as explained.


If you don't use my debug function but use your loop with echo, the following test script yields also something buggy :

<?php

$xml = new DOMDocument();
$xml->loadXml('<balise>
  <ata>2013-08-20T17:13:25</ata>
  <atd>2013-08-20T17:15:03</atd>
  <callSequence>1</callSequence>
  <actualCallSequence>2</actualCallSequence>
  <clauseInfo>
    <clause>LIV_CFM</clause>
    <description>Conforme</description>
  </clauseInfo>
</balise>');

foreach ($xml->documentElement->childNodes as $childnode) {
    echo get_class($childnode) . "\n";
}

?>  

yields

DOMText
DOMElement
DOMText
DOMElement
DOMText
DOMElement
DOMText
DOMElement
DOMText
DOMElement
DOMText

Best regards,
   Laurent Lyaudet
 [2015-07-08 17:31 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-07-08 17:31 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

A slight modification of your first test script (see
<http://3v4l.org/VmfC7>) shows that the grand-children are not
iterated over. "LIV_CFM" and "Conforme" are yield by ::nodeValue
(what may be considered a bug, but that should be reported in a
separate ticket).

The results of the second test script also do not contain
grand-children; there are 5 children and these are printed as
DOMElement, while the TextNodes are built from the whitespace
between the elements. Simply strip all whitespace, and you get the
desired result (see <http://3v4l.org/YsTE8>).
 [2015-07-08 21:00 UTC] Laurent dot Lyaudet at gmail dot com
Hi, 

Thanks for the feedback and showing me that the bug was because of incorrect node value and not because of additionnal DOMText elements.
I opened another bug report for correcting node value instead, as you suggested.

Best regards,
   Laurent Lyaudet
 [2015-07-08 21:23 UTC] cmb@php.net
Thanks, Laurent, for opening the other ticket, and sorry for the
late reply to this one.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 08:01:30 2024 UTC