php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20639 DomElement->get_elements_by_tagname() doesn't work
Submitted: 2002-11-26 02:34 UTC Modified: 2002-11-26 09:41 UTC
From: chris at improbable dot org Assigned:
Status: Closed Package: DOM XML related
PHP Version: 4CVS-2002-11-26 (stable) OS: Mac OS X 10.2.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: chris at improbable dot org
New email:
PHP Version: OS:

 

 [2002-11-26 02:34 UTC] chris at improbable dot org
It looks like DomElement->get_elements_by_tagname() should work like DomDocument->get_elements_by_tagname() but returns nothing:
<?php

	header("Content-Type: text/plain");
	
$src = <<< _END
<html>
<head><title> Test </title></head>
<body> 
<h1>Test</h1>
<ul>
<li>foo</li>
<li>bar</li>
</ul>
</body>
</html>
_END;

$doc = domxml_open_mem($src);

$n = $doc->get_elements_by_tagname("li");
print get_class($doc) . "->get_elements_by_tagname:\n";
print_r($n);

$ctx = $doc->xpath_new_context();

$Bodies = $doc->get_elements_by_tagname("body");
foreach ($Bodies as $Body) {
	print get_class($Body) . "->get_elements_by_tagname:\n";
	print_r($Body->get_elements_by_tagname("li"));
	
	$xp = getXPath($Body) . "/*/li";
	print get_class($ctx) . " xpath ($xp):\n";
	$res = $ctx->xpath_eval($xp);
	print_r($res->nodeset);

}

function getXPath($node) {
	/* node id is held in a property named '1', this is
	illegal in php so we use a workaround */
	$one = '1';
	$xpath = '';
	while ($parent = $node->parent_node()) {
		$siblings = $parent->child_nodes();
		$index = 1;
		foreach ($siblings as $sibling) {
			if ($sibling->type != XML_ELEMENT_NODE || $sibling->tagname != $node->tagname) continue;
				if ($sibling->$one == $node->$one) {
					$xpath = '/' . $node->tagname . '[' . $index . ']' . $xpath;
					break;
				}
			$index++;
		}
		$node = $parent;
	}

	return $xpath;
}

?>

returns 
domdocument->get_elements_by_tagname:
Array
(
    [0] => domelement Object
        (
            [type] => 1
            [tagname] => li
            [0] => 3
            [1] => 1985152
        )

    [1] => domelement Object
        (
            [type] => 1
            [tagname] => li
            [0] => 4
            [1] => 2523424
        )

)
domelement->get_elements_by_tagname:
Array
(
)
XPathContext xpath (/html[1]/body[1]/*/li):
Array
(
    [0] => domelement Object
        (
            [type] => 1
            [tagname] => li
            [0] => 3
            [1] => 1985152
        )

    [1] => domelement Object
        (
            [type] => 1
            [tagname] => li
            [0] => 4
            [1] => 2523424
        )

)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-26 09:41 UTC] chregu@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 18:01:35 2024 UTC