2

I have tried to find a way to "bookmark" an element on a page so i will later on be able to check on it. everything I've tried so far lead me to some problem so I've came to the conclusion that finding the absolute path might be the only way.

I am new to xpath or DOMdocument in general so i might have the naming wrong however by absolute path i mean something like:

"/html/body/div[1]/div[1]/div[2]/span[2]"

lets say i use:

$element_with_something = $xpath->query('(//*[text()= "something"])');

This gave me an element. now my question is there an easy way of finding it's absolute path. if not. then i will probably have to recursion my way up the tree.

if so how can i check if an element has a parent?

I know i can use hasChildNodes() to find child but is there a way to find out if there is a parent ? or any other way to break the recursion ones it hit the top of the tree ?

Neta Meta
  • 4,001
  • 9
  • 42
  • 67

1 Answers1

2

You might be looking for DOMNode::getNodePath(). A quick example:

$xml = <<<XML
<blaah1 name="whatever">
    <gender name="male">

        <example1 baseurl="male/86644/">
            <x u="lol.png"/>
            <x u="haha.png"/>
            <x u="name.png"/>
        </example1>

        <example2 baseurl="male/27827/">
            <x u="page.png"/>
            <x u="examp.png"/>
            <x u="bottom.png"/>
        </example2>
    </gender>
</blaah1>
XML;

$doc = new DOMDocument();
$doc->loadXML($xml);
$xp = new DOMXPath($doc);
foreach($xp->query('//node()') as $node ) {

    echo $node->getNodePath(), "\n";

}

And it's output:

/blaah1
/blaah1/text()[1]
/blaah1/gender
/blaah1/gender/text()[1]
/blaah1/gender/example1
/blaah1/gender/example1/text()[1]
/blaah1/gender/example1/x[1]
/blaah1/gender/example1/text()[2]
/blaah1/gender/example1/x[2]
/blaah1/gender/example1/text()[3]
/blaah1/gender/example1/x[3]
/blaah1/gender/example1/text()[4]
/blaah1/gender/text()[2]
/blaah1/gender/example2
/blaah1/gender/example2/text()[1]
/blaah1/gender/example2/x[1]
/blaah1/gender/example2/text()[2]
/blaah1/gender/example2/x[2]
/blaah1/gender/example2/text()[3]
/blaah1/gender/example2/x[3]
/blaah1/gender/example2/text()[4]
/blaah1/gender/text()[3]
/blaah1/text()[2]
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Yes actually it looks pretty much as you've had it outlines in your question. There is something else, let me try that. Are you using that with HTML documents here? – hakre Dec 23 '12 at 21:47
  • i am basically pulling pages and locating price on them. so i can later on check it basically any page with price would work. I've tried it and it seems good – Neta Meta Dec 23 '12 at 21:54
  • Yes that this thingy is a good choice. If you need to bookmark within the same document, it's also fine to set the ID attribute on the elements you want to bookmark (or if those are set, to get that ID value). – hakre Dec 23 '12 at 22:09
  • Is there any equivalent to the above for javascript ? – Neta Meta Dec 23 '12 at 23:32
  • @NetaMeta, This can be done in pure XSLT. See: http://stackoverflow.com/questions/4746299/generate-get-xpath-from-xml-node-java – Dimitre Novatchev Dec 24 '12 at 00:19