Thursday, May 5, 2011

php xpath - get node position

I have the following stripped down php code...

foreach ($xpath->query('/html/body//a')} as $queryitem) {
   $nodeposition = ???;
   $parentposition = ???;
}

...from this is it possible to find the position of each node and the position its parent?

for example:

0..<html>
1....<head>
2......<title>
3......<meta>
4......<meta>
5....<body>
6......<div>
7........<a>

This would produce a kind-of ID for each tag and the ID of the parent tag which it joins to.

So the results would be = 0|,1|0,2|1,3|1,4|1,5|0,6|5,7|6

From stackoverflow
  • I don't believe you will be able to get the position (read xpath) within the dom unless you do some fancy coding yourself.

  • You need to be aware of the full DOM tree to solve this problem. But with XPath you just get certain subtrees that match the path. You would need to walk the tree up one by one and retrieve the information for every element matched by the XPath. But that might be inefficient. It would probably be better if you walk the tree from the root down and count the number of nodes.

  • If by "position" you mean the count of preceding notes (as you seem to imply in your sample), then this would work (probably not at top-notch efficiency, but nevertheless):

    foreach ($xpath->query('/html/body//a') as $queryitem) {
       $nodeposition = count($xpath->query('preceding::*', $queryitem));
    
       $parent = $xpath->query('parent::*', $queryitem);
       if ($parent->length == 1)
         $parentposition = count($xpath->query('preceding::*', $parent->item(0)));
       else
         $parentposition = -1; // or whatever
    }
    

    Not tested, but you get the idea.

    An alternative would be to code up an XSLT identity transform that adds the position to all nodes in the document as an attribute. You could then read the attribute in you PHP code.

  • The most simple way of dealing with XML in PHP is using the SimpleXML class and loop using the Children attribute. See http://nl2.php.net/manual/en/function.simplexml-element-children.php

0 comments:

Post a Comment