GNOME Bugzilla – Bug 322890
xpath doesn't work correctly when there are schemas specified in xml file
Last modified: 2005-12-01 08:30:03 UTC
Distribution/Version: PLD Ac test1.xml contains schema specs; test2.xml doesn't. a.py does the same thing on test1.xml as b.py on test2.xml. xpath in a.py doesn't find anything while in b.py does. $ cat test1.xml <?xml version="1.0" encoding="UTF-8"?> <epp xmlns="http://www.eurid.eu/xml/epp/epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:contact="http://www.eurid.eu/xml/epp/contact-1.0" xmlns:domain="http://www.eurid.eu/xml/epp/domain-1.0" xmlns:eurid="http://www.eurid.eu/xml/epp/eurid-1.0" xmlns:nsgroup="http://www.eurid.eu/xml/epp/nsgroup-1.0" xsi:schemaLocation="http://www.eurid.eu/xml/epp/epp-1.0 epp-1.0.xsd http://www.eurid.eu/xml/epp/contact-1.0 contact-1.0.xsd http://www.eurid.eu/xml/epp/domain-1.0 domain-1.0.xsd http://www.eurid.eu/xml/epp/eurid-1.0 eurid-1.0.xsd http://www.eurid.eu/xml/epp/nsgroup-1.0 nsgroup-1.0.xsd"> <response> <result code="1000"> <msg>Command completed successfully</msg> </result> <trID> <clTRID>TRID-0001</clTRID> <svTRID>eurid-3650743</svTRID> </trID> </response> </epp> $ cat a.py #!/usr/bin/python -u import libxml2 doc = libxml2.parseFile("test1.xml") ctxt = doc.xpathNewContext() res = ctxt.xpathEval("//result/msg") print res; libxml2.cleanupParser() $ python a.py [] $ cat test2.xml <?xml version="1.0" encoding="UTF-8"?> <epp> <response> <result code="1000"> <msg>Command completed successfully</msg> </result> <trID> <clTRID>TRID-0001</clTRID> <svTRID>eurid-3650743</svTRID> </trID> </response> </epp> $ cat b.py #!/usr/bin/python -u import libxml2 doc = libxml2.parseFile("test2.xml") ctxt = doc.xpathNewContext() res = ctxt.xpathEval("//result/msg") print res; libxml2.cleanupParser() $ python b.py [<xmlNode (msg) object at 0x302e8288>] $
Unfortunatly, you just left the IRC channel before I could answer your question. But this is not a bug. 'Standalone' Xpath (e.g. not inside xslt), doesn't know anything about the default namespace. Any node name without a prefix is considered to be in the 'null' namespace (see http://www.w3.org/TR/xpath#node-sets). So b.py works because there's no namespace in the source xml. In a.py, you can either use local-name() or else you need to specify a namespace for the default one, for example add before the xpathEval: root = doc.getRootElement() defaultNS = root.searchNs(root,None).content ctxt.xpathRegisterNs('default',defaultNS) res = ctxt.xpathEval("//default:result/default:msg")
Thanks, after registering some default namespace it works nicely! Sorry for a noise.