XPath help?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
OK, not sure what I'm missing here.  I'm using PHP 5.2.5.  I have an
XHTML document successfully loaded into a DOMDocument object $doc
(although for some reason loadXML() complains that   is not a
defined entity, even though the !DOCTYPE is pointing to an HTML DTD
that defines it).  The document is navigable, with the DOM methods, I
can output it with saveXML() and all looks good, etc.

So I try to extract some nodes via XPath, and I can't find anything.

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//td');

That should find all the <td> elements.  According to both $doc-
Quoted text here. Click to load it
those.  But it returns the empty set.

$xpath->query('/') returns the document node as expected.
$xpath->query('/*') returns the html element as expected.
$xpath->query('/html') returns the empty set.
$xpath->query('//html') also returns the empty set.

No matter what element name I use, XPath won't find it if I search for
it; but if I search via wildcards and then examine the name of the
nodes I find, they match exactly the strings I'm searching for.

None of the names are qualified in the document, btw.

So I'm a little confused.

Can anyone help?

Re: XPath help?

Ok, never mind, this was the old namespace mapping issue.

So why is it that XPath processors don't use the default namespace
information declared by the xmlns attribute of the root element?

Re: XPath help?

Quoted text here. Click to load it


If I have this statement in my xhtml page:

<html xmlns=3D"http://www.w3.org/1999/xhtml " lang=3D""

then what would be my namespace? I don't know what I would have to
type inside the ->xpath() if I don't have a namespace declared (since
I don't want to change all my tags).

I hope you can help.

Site Timeline