libxml2 w/ xpath python bindings

Discussion in 'Python' started by Brian Donovan, Jan 23, 2004.

  1. Hi All,

    I'm trying to get xpath to work with the libxml2 python bindings.
    I'm using the following

    doc = libxml2.parseFile(filename)
    result = doc.xpathEval('//*')

    My test XML file has 10 nodes in it and I'm trying to get an element named
    'element' (from an relaxng schema).

    If I use the xpath expression '//*' I'll get a list with 10 nodes
    including the element element as expected. If I try '//element' I'm
    getting an empty list with no errors. Any idea where I could be going
    wrong?

    Brian
     
    Brian Donovan, Jan 23, 2004
    #1
    1. Advertisements

  2. Turns out the issue was with the XML namespaces. The document had a
    defualt namespace which had to be declared:

    xmlDoc = libxml2.parseFile(base + '/' + curfile)
    ctxt = xmlDoc.xpathNewContext()
    ctxt.xpathRegisterNs('rng',"http://relaxng.org/ns/structure/1.0")

    elements = ctxt.xpathEval('//rng:element/@name')
    for e in elements:
    rnc.elements.append(e.content)

    On Fri, 23 Jan 2004 18:58:34 +0000, Brian Donovan wrote:

    > Hi All,
    >
    > I'm trying to get xpath to work with the libxml2 python bindings.
    > I'm using the following
    >
    > doc = libxml2.parseFile(filename)
    > result = doc.xpathEval('//*')
    >
    > My test XML file has 10 nodes in it and I'm trying to get an element named
    > 'element' (from an relaxng schema).
    >
    > If I use the xpath expression '//*' I'll get a list with 10 nodes
    > including the element element as expected. If I try '//element' I'm
    > getting an empty list with no errors. Any idea where I could be going
    > wrong?
    >
    > Brian
     
    Brian Donovan, Jan 23, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen R. Plasser

    libxml2 Python bindings error

    Juergen R. Plasser, Jul 3, 2003, in forum: Python
    Replies:
    0
    Views:
    485
    Juergen R. Plasser
    Jul 3, 2003
  2. Logan
    Replies:
    1
    Views:
    470
    Logan
    Nov 21, 2003
  3. Maxim Khesin

    libxml2/xpath

    Maxim Khesin, Dec 16, 2004, in forum: Python
    Replies:
    5
    Views:
    7,722
    Frans Englich
    Dec 16, 2004
  4. OpenMacNews
    Replies:
    0
    Views:
    384
    OpenMacNews
    Apr 3, 2005
  5. Replies:
    2
    Views:
    1,204
    Jean-Roch SOTTY
    Dec 5, 2005
  6. Andrew Marlow
    Replies:
    2
    Views:
    558
    Andrew Marlow
    Jan 18, 2007
  7. shaun roe

    python libxml2 xpath extensions

    shaun roe, Nov 11, 2007, in forum: XML
    Replies:
    2
    Views:
    3,051
    shaun roe
    Nov 13, 2007
  8. Steve Ball
    Replies:
    0
    Views:
    3,015
    Steve Ball
    Nov 24, 2008
Loading...