Re: [xml] Xpath issues with libxml2

On Thu, Feb 12, 2015 at 11:36:12AM -0500, Alex Boese wrote:
I kinda solved my own problem. I'm posting an example of what works, since I had difficulty establishing 

import libxml2
import sys

XML1 = """<f:Foo xmlns:f="";><b:Bar

xml_parser_options = libxml2.XML_PARSE_RECOVER + libxml2.XML_PARSE_NONET
size = sys.getsizeof(XML1)
doc = libxml2.readMemory(XML1,size,None,'UTF-8',xml_parser_options)

context = doc.xpathNewContext()

print "test 1"
res = context.xpathEval('/*[local-name()="Foo"]/*[local-name()="Bar"]')
i = 0
for node in res:
    print i,':',node
    i = i + 1

#print "test 2"
#res = context.xpathEval('/f:Foo/b:Bar')
#i = 0
#for node in res:
#     print i,':',node
#     i = i + 1

Test 1
0 : <b:Bar xmlns:b="";>foobar</b:Bar>

Of course, if you comment in the commented out portions you get test 2 which
returns the exact same thing. The point I'm making above is that it might not even be a requirement to 
register each namespace! It depends really on what kind of xpath you're using.

Of course, feel free to bash my answer and tell me I'm wrong.

  You only need to register in the XPathcontext the namespaces that
you are using in the XPath queries related to this context. Also what
prefix you use is of no matter, XPath will to the matching on the
namespace name (the URL for the namespace), never the prefix,


I'm thinking it should look something like this, assuming Python language:

Import xmlsec
Import libxml2


  def test_get_xml_fragment(self,xpath,ns):
       ret = None
       context = self.doc.xpathNewContext()
       if ns is None:

       res = context.xpathEval(xpath)
       i = 0
       for node in res:
            print i,':',node
            i = i + 1

Please note there is an object in the mix, and I can get the whole to work great without namespaces. One 
good example of how Python should be handling namespaces in this case would be great. Even telling me 
it's forever broken...this too would be good to know. (Also, this is not an ideal example...I get this. 
I want something that hobbles before I get something hat soars.)

Thanks in advance.

Sent from my Planet

Message: 2
Date: Fri, 30 Jan 2015 09:03:40 -0600
From: Ross Reedstrom <reedstrm rice edu>
To: xml gnome org
Subject: Re: [xml] Xpath issues with libxml2
Message-ID: <20150130150340 GA28262 rice edu>
Content-Type: text/plain; charset=us-ascii

Alex -
With out examples of what you've tried, it's hard to diagnose the problem.
However, seeing 'namespaces' and 'never returns anything' makes me think you're
having issues with the default namespace concept. While XML documents have a
default namespace, XPaths do not. Once you use namespaces in an xml document,
all your xpaths will need to use namespace declarations for all the path parts,
even for tags that are defaulted in the document. So, you'll need to declare
a namespace prefix that matches the default namespace in the doc. Give us
a small example that you've tried that doesn't work, we'll fix it.


On Fri, Jan 30, 2015 at 08:44:15AM -0500, Alex Boese wrote:
Forgive me if this is a deprecated approach (as I am not fully aware), but I was utilizing "default" 
libxml2 bindings (not lxml) in Python to retrieve xml fragments via xpath functions. Normally this 
seems to work fine, but with namespace declarations this seems especially problematic as nothing ever 
returns and nothing errors. Would it be possible to confirm what correct functions and order of 
operations would be for this? Even if the example is C, I can translate that to Python.

Ross Reedstrom, Ph.D.                                 reedstrm rice edu
Systems Engineer & Admin, Research Scientist        phone: 713-348-6166
Connexions                    fax: 713-348-3665
Rice University MS-375, Houston, TX 77005
GPG Key fingerprint = F023 82C8 9B0E 2CC6 0D8E  F888 D3AE 810E 88F0 BEDE


Subject: Digest Footer

xml mailing list
xml gnome org


End of xml Digest, Vol 128, Issue 2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>


Subject: Digest Footer

xml mailing list
xml gnome org


End of xml Digest, Vol 129, Issue 1

xml mailing list, project page
xml gnome org

Daniel Veillard      | Open Source and Standards, Red Hat
veillard redhat com  | libxml Gnome XML XSLT toolkit | virtualization library

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