libxml2/xpath

Frans Englich frans.englich at telia.com
Thu Dec 16 07:59:53 EST 2004


On Thursday 16 December 2004 12:29, Maxim Khesin wrote:
> I do not believe it is... You can see the doc by clicking on the link.
> Does it have to be?

No, but your XPath statements must match the namespace, no matter what it is. 
The document do have a namespace -- as XHTML should: 

<html xmlns="http://www.w3.org/1999/xhtml">

The problem with this:

mydoc.xpathEval('/html')

is that it tries to match a top element whose local name is "html" and whose 
namespace is null, but in the source document, the local name is "html" but 
the namespace is not null, it's "http://www.w3.org/1999/xhtml" --> they don't 
match.

The solution is to have a matching namespace, such that the whole qualified 
name matches. AFAIK, it is done like this in libxml2:

    # confDocument is a libxml2 document, from parseFile() etc
    xp = confDocument.xpathNewContext()
    xp.xpathRegisterNs("xhtml", "http://www.w3.org/1999/xhtml")
    dirElement = xp.xpathEval( "/xhtml:html" )
    

Cheers,

		Frans



More information about the Python-list mailing list