| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             [2002-11-26 09:41 UTC] chregu@php.net
  | 
    |||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 09:00:01 2025 UTC | 
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 ) )