tags:

views:

43

answers:

3

I have seen examples of usage of a getNodePath() method on PHP DOM objects.

see:

http://www.php.net/manual/en/class.domdocument.php#91072

However i cannot find the documentation for that method.

I have been going around in circles in the DOM Docs.

http://www.php.net/manual/en/book.dom.php

Any ideas?

A: 

You can create your own getNodeXPath() like this:

<?php
/**
* result sample : /html[1]/body[1]/span[1]/fieldset[1]/div[1]
* @return string
*/
function getNodeXPath( $node ) {    
    $result='';
    while ($parentNode = $node->parentNode) {
        $nodeIndex=-1;
        $nodeTagIndex=0;
        do {
            $nodeIndex++;
            $testNode = $parentNode->childNodes->item( $nodeIndex );

            if ($testNode->nodeName==$node->nodeName and $testNode->parentNode->isSameNode($node->parentNode) and $testNode->childNodes->length>0) {
                //echo "{$testNode->parentNode->nodeName}-{$testNode->nodeName}-{}<br/>";
                $nodeTagIndex++;
            }

        } while (!$node->isSameNode($testNode));

        $result="/{$node->nodeName}[{$nodeTagIndex}]".$result;
        $node=$parentNode;
    };
    return $result;
}
?>

This is defined here.

shamittomar
A: 

The example you linked to indicates which objects have this method:

        case ($obj instanceof DOMDocument):
            ... $obj->getNodePath() ...
            ...
        case ($obj instanceof DOMElement):
            ... $obj->getNodePath() ...
            ...

So, DOMDocument and DOMElement instances have the getNodePath() method. However, I wasn´t able to find any documentation in the official PHP docs, but I found a blog post apparently by the implementor of that method: http://blog.liip.ch/archive/2006/07/16/added-domnode-getnodepath.html

Max
Thanks for that link. The author implies that it is on the DOMNode class. Which fits cos it is the parent of both DOMDocument and DOMElement. However, the docs don't mention getNodePath() http://www.php.net/manual/en/class.domnode.phpSo problem is kind of solved.(I should have said 'class' in my question - cos that was what i was trying to work out.).
JW