1
votes

I have a javascript popup that "successfully" employs php to load another page as a DOMDocument and "successfully" finds a element by id to display its text using nodeValue...BUT...the nodeValue call returns NOT the text inside the desired element but the text inside an anchor tag with the same name. Here's some of the code:

the html and php that seemingly "works":

<span style="position:relative;"><span id="favelas" class="popup">

<?php

// Create a new DOMDocument object
$doc = new DOMDocument;

// enable user error handling
libxml_use_internal_errors(true);

// Validate our document before referring to the id
$doc->validateOnParse = true;

// Load the key terms and identifications html file
$doc->loadHtml(file_get_contents('http://teachers.dadeschools.net/jzoeller/APHG/0-Key-Terms-Identifications.html'));

// Print in readable form the content the element by id
print_r($doc->getElementById('favela')->nodeValue);

?>

</span><a href="javascript:void(null);" onMouseover="ShowPop('favelas');" onMouseout="HidePop('favelas');">favelas</a></span>

Now, what "should" be displayed is the definition of the term favela from the page referenced in the code above. What I'm getting is merely the word "favela".

Here's some more code, this time from the page loaded by the php:

<tr>
<td><a name="favela">
favela</a></td>
<td class="def" id="favela">A shantytown or slum, especially in Brazil.</td>
<td>07</td>
<td>06</td>
</tr>

Debugging with var_dump gives me this:

object(DOMElement)#1 (17) { ["tagName"]=> string(1) "a" ["schemaTypeInfo"]=> >NULL ["nodeName"]=> string(1) "a" ["nodeValue"]=> string(8) " favela" >["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" >["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) >"(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" >["previousSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" >["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL >["prefix"]=> string(0) "" ["localName"]=> string(1) "a" ["baseURI"]=> NULL >["textContent"]=> string(8) " favela" }

This seems to be saying that its got the anchor named "favela" and NOT the td named "favela". What gives?!

1
Why is this tagged "Javascript". Isn't this a PHP question about DOMDocument implemented in PHP? - jfriend00
I don't know DOMDocument that well, but I think it handles html as html40 loose and there the name attribute of an a element is an ID attribute (the ID attribute does not need to have the name id). As of that you will find the a. - t.niese
Okay, so, as I suspected, its tripping on the anchor; so how do I skip the anchor and get the second instance of the id, in this case "favela". Also, thanks for the explanation. I hope my solution is as concise! - John Zoeller
You might have success using DOMXPath searching for //[@id='favela'] but as I said, I don't know DOMDocument that well, so iI neither know if my assumption is really correct, nor if @id will suffer from the same problem. - t.niese

1 Answers

1
votes

You can use a DOMXPath query instead of getElementById() to dodge the name attribute and target only the element with an id attribute of "favela":

$xpath = new DOMXPath($doc);
$favelaElement = $xpath->query('//*[@id="favela"]')->item(0);

print_r($favelaElement->nodeValue);

Output:

A shantytown or slum, especially in Brazil.