note 28061 added to function.xpath-eval

From: Date: Tue, 31 Dec 2002 16:12:44 +0000
Subject: note 28061 added to function.xpath-eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-41517@lists.php.net to get a copy of this message
chregu wrote: $ctx->xpath_eval("xpath",$node); this is another way to get xpath??? doesn't work for me; always returns empty: /* function getXPath I added namespace support to the getXPath function above. Before using the xpath, you must call xpath_register_ns() like this: $ctx=xpath_new_context($domDocument); $ctx->xpath_register_ns('xm', 'http://www.xmlmake.com/xm'); $nodes = $ctx->xpath_eval($xpath); */ 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()) { $prefix = $node->prefix(); if (! empty( $prefix ) ){ $prefixStr = $prefix .':'; } $siblings = $parent->child_nodes(); $index = 1; foreach ($siblings as $sibling) { // if ($sibling->type != XML_ELEMENT_NODE || $sibling->tagname != $node->tagname) continue; if ($sibling->type != XML_ELEMENT_NODE || $sibling->tagname != $node->tagname || $sibling->prefix() != $prefix )continue; if ($sibling->$one == $node->$one) { $xpath = '/' . $prefixStr . $node->tagname . '[' . $index . ']' . $xpath; break; } $index++; } $node = $parent; } return $xpath; }// getXPath -- http://www.php.net/manual/en/function.xpath-eval.php http://master.php.net/manage/user-notes.php?action=edit+28061 http://master.php.net/manage/user-notes.php?action=delete+28061 http://master.php.net/manage/user-notes.php?action=reject+28061

« previous php.notes (#41517) next »