PHPverse 2025

Voting

The Note You're Voting On

fantasyman3000 at gmail dot com
16 years ago
In response to "simlee at indiana dot edu",
- First of all thanks for sharing your funciton.
- It didn't work for me so i rewrite it from scratch using different method.

Here is the new version, hope it helps someone :

<?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;
}
?>

By Sina.Salek.ws

<< Back to user notes page

To Top