On 18/04/2008, Alistair Leslie-Hughes <[EMAIL PROTECTED]> wrote: > Hi, > > When use xmlXPathEval with the query "/root/WEB:Site" on the following xml > > <?xml version=\"1.0\"?> > <root xmlns:WEB='http://www.winehq.org'> > <WEB:Site version=\"1.0\" /> > </root> > > I get the following error > XPath error : Undefined namespace prefix > xmlXPathEval: evaluation failed > > Using the same xml and query in MSXML it works. > > Can someone explain what I am doing wrong? > > Best Regards > Alistair Leslie-Hughes
You need to register the namespace prefix/uri pair on the xpath context. See: http://xmlsoft.org/html/libxml-xpathInternals.html#xmlXPathRegisterNs Some example xpath code: http://xmlsoft.org/examples/xpath1.c Or python version, which is brief enough for email: >>> import libxml2 >>> doc = libxml2.parseDoc("""<?xml version="1.0"?> ... <root xmlns:WEB='http://www.winehq.org'> ... <WEB:Site version="1.0"/> ... </root>""") >>> ctx = doc.xpathNewContext() >>> ctx.xpathRegisterNs("WEB", "http://www.winehq.org") 0 >>> ctx.xpathEval("count(//WEB:*)") 1.0 This one really needs to go on an FAQ thingy really. Martin _______________________________________________ xml mailing list, project page http://xmlsoft.org/ [email protected] http://mail.gnome.org/mailman/listinfo/xml
