tags:

views:

32

answers:

1

Considering this HTML code:

<div id="something_unique">
 <form action="http://the_needed_link"&gt;
 </form>
</div>

How do we get the needed link assuming that there is a lot of form tags on the page but we do only need the one that is in a div that has a unique id?

+4  A: 

I believe this should do it:

//div[@id="something_unique"]/form/@action

EDIT

Sorry, I was off by one char; a missing slash between the element and the attribute. Here's a full example:

$doc = new DOMDocument( '1.0', 'utf-8' );
$doc->loadXML(
    '<root>
    <div id="something_unique">
        <form action="http://the_needed_link"&gt;
        </form>
    </div>
    </root>
    '
);

$xpath = new DomXPath( $doc );
$result = $xpath->query( '//div[@id="something_unique"]/form/@action' );

var_dump( $result->item( 0 )->nodeValue );
fireeyedboy
Almost. `//div[@id="something_unique"]/form/@action`
Tomalak
@Tomalak, you are right! Thanks. I just tested it myself, because I wasn't sure. I'll update my answer with a full example.
fireeyedboy
That's better. :-) +1
Tomalak
+1 Right answer.
Alejandro