Re: [xml] Namespace Query Issue



On 18/04/2008, Alistair Leslie-Hughes <leslie_alistair hotmail com> 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



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]