#20948 [NEW]: DomElement->get_elements_by_tagname is now recursive

From: Date: Wed, 11 Dec 2002 23:15:20 +0000
Subject: #20948 [NEW]: DomElement->get_elements_by_tagname is now recursive
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-27437@lists.php.net to get a copy of this message
From: dave@thequestion.net Operating system: FreeBSD PHP version: 4.3.0RC3 PHP Bug Type: DOM XML related Bug description: DomElement->get_elements_by_tagname is now recursive The function DomElement->get_elements_by_tagname() is now recursive. It is returning matching elements from it's children nodes as well. In 4.2.3, this function would only return matching elements in it's node. The following code (with output) shows the difference. #!/usr/bin/php -q <?php $xmlsource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; $xmlsource .= "<root comment=\"root node\">"; $xmlsource .= " <one comment=\"one\">"; $xmlsource .= " <two comment=\"first two, count should be 1\">"; $xmlsource .= " <three comment=\"three\">"; $xmlsource .= " </three>"; $xmlsource .= " </two>"; $xmlsource .= " <four comment=\"four\">"; $xmlsource .= " <two comment=\"second two\">"; $xmlsource .= " </two>"; $xmlsource .= " </four>"; $xmlsource .= " </one>"; $xmlsource .= "</root>"; $doc = domxml_open_mem($xmlsource); echo "==========================================================\r\n"; echo "PHP Version: ".phpversion()."\r\n"; echo "==========================================================\r\n"; echo sprintf("\$doc->get_elements_by_tagname(\"two\"): %d\r\n", count($doc->get_elements_by_tagname("two"))); echo "----------------------------------------------------------\r\n"; echo " Count | Node | Comment\r\n"; echo "----------------------------------------------------------\r\n"; // Set root node $node_root = $doc->document_element(); // Find node_one $array_nodes = $node_root->get_elements_by_tagname("one"); if (count($array_nodes) > 0) { $node_one = $array_nodes[0]; // echo sprintf("[%d] node_one comment: %s\r\n", count($array_nodes), $node_one->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_one", $node_one->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_one", "Not Found"); } // Find node_firstTwo $array_nodes = $node_one->get_elements_by_tagname("two"); if (count($array_nodes) > 0) { $node_firstTwo = $array_nodes[0]; // echo sprintf("[%d] node_firstTwo comment: %s\r\n", count($array_nodes), $node_firstTwo->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_firstTwo", $node_firstTwo->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_firstTwo", "Not Found"); } // Find node_four $array_nodes = $node_root->get_elements_by_tagname("four"); if (count($array_nodes) > 0) { $node_four = $array_nodes[0]; // echo sprintf("[%d] node_four comment: %s\r\n", count($array_nodes), $node_four->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_four", $node_four->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_four", "Not Found"); } // Find node_secondTwo if ($node_four) { $array_nodes = $node_four->get_elements_by_tagname("two"); if (count($array_nodes) > 0) { $node_secondTwo = $array_nodes[0]; // echo sprintf("[%d] node_secondTwo comment: %s\r\n", count($array_nodes), $node_secondTwo->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_secondTwo", $node_secondTwo->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_secondTwo", "Not Found"); } } echo "==========================================================\r\n"; ?> Output for PHP 4.2.3: ========================================================== PHP Version: 4.2.3 ========================================================== $doc->get_elements_by_tagname("two"): 2 ---------------------------------------------------------- Count | Node | Comment ---------------------------------------------------------- 1 | node_one | one 1 | node_firstTwo | first two, count should be 1 0 | node_four | Not Found ========================================================== Output for PHP 4.3.0RC3: ========================================================== PHP Version: 4.3.0RC3 ========================================================== $doc->get_elements_by_tagname("two"): 2 ---------------------------------------------------------- Count | Node | Comment ---------------------------------------------------------- 1 | node_one | one 2 | node_firstTwo | first two, count should be 1 1 | node_four | four 1 | node_secondTwo | second two ========================================================== Hopefully you can see what I'm trying to show here. The fact that 4.3.0RC3 finds the node_four and thus the node_secondTwo shows that DomElement->get_elements_by_tagname is recursive. Since PHP support for DOM XML is experimental, this might be a feature rather than a bug. If that's the case, maybe a parameter could be added to the function to select whether or not to traverse deeper into the tree. Thanks Dave Note: this was tested with libxml2-2.4.21 installed. Everything on the system is identical for each run above except for the php binary. -- Edit bug report at http://bugs.php.net/?id=20948&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=20948&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=20948&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=20948&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=20948&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=20948&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=20948&r=support Expected behavior: http://bugs.php.net/fix.php?id=20948&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=20948&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=20948&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=20948&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=20948&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=20948&r=dst IIS Stability: http://bugs.php.net/fix.php?id=20948&r=isapi

« previous php.bugs (#27437) next »