PHP Script Display an array of XPaths that Point to all nodes in nodelist object
I’m looking for one at the moment… i will either write one or find one
this php script displays an array of xpaths that point to all nodes in nodelist (DOMNodeList) object
….???….
Here’s some leads on this:
http://www.dpawson.co.uk/xsl/sect2/N6077.html#d177e20
which came from
http://www.stylusstudio.com/xsllist/200210/post40530.html#
here’s a rough set of functions that i have written to be getting on with. Note, they do not produce unique xpaths and the document seems to appear in the path too with a hash on the front of it.
I guess to get ‘as-unique-as-possible’ xpaths for each element node you’d have to make a record of each node’s position() :
function recursivley_add_node_parents($use_4_context_node_obj, $nodes_ancestors_array)
{
//WARNING - RECURSIVE FUNCTION
$parent_node_obj = $use_4_context_node_obj->parentNode;
if (is_object($parent_node_obj))
{
$nodes_ancestors_array[] = $parent_node_obj->nodeName;
return $this->recursivley_add_node_parents($parent_node_obj, $nodes_ancestors_array);
}
else {
return $nodes_ancestors_array;
}
}
function get_xpath_of_this_node($use_4_context_node_obj)
{
//climb up the tree til you get to the top
$nodes_xpath =”;
$nodes_ancestors_array = array();
$nodes_ancestors_array[] = $use_4_context_node_obj->nodeName;
$nodes_ancestors_array = recursivley_add_node_parents($use_4_context_node_obj, $nodes_ancestors_array);
$nodes_ancestors_array = array_reverse($nodes_ancestors_array);
$num_ancestors = count($nodes_ancestors_array);
for ($idx = 0; $idx < $num_ancestors; $idx++)
{
$nodes_xpath .= $step_delimter . $nodes_ancestors_array[$idx];
$step_delimter=’/';
}
return $nodes_xpath;
}
A coupel of firefox plugins may help:
XPath Checker
https://addons.mozilla.org/en-US/firefox/addon/1095
and
XPather
https://addons.mozilla.org/en-US/firefox/addon/1192