Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3444056
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:52:03+00:00 2026-05-18T08:52:03+00:00

I’m trying to learn how to do XPath queries from Python using this sample

  • 0

I’m trying to learn how to do XPath queries from Python using this sample XML file: http://pastie.org/1333021 I just added a namespace to it because my actual application uses it.

Basically, I want to execute a top level query that returns a subset of nodes and then query the subset (on a much larger scale than this example)

So this is my code to first find all <food> nodes and then iterate over the description of each one.

#!/usr/bin/python2

import libxml2

doc = libxml2.parseFile("simple.xml")
context = doc.xpathNewContext()

context.xpathRegisterNs("db", "http://examplenamespace.com")
res = context.xpathEval("//db:food")

for node in res:
    # Query xmlNode here
    print "Got Food Node:"
    desc = node.xpathEval('db:description') # this is wrong?
    print desc

So it’s essentially a namespace problem, if I remove the xlns attribute from the XML file and use just basic XPATH queries without db: it works fine. The top query //db:food works fine, but the second one fails evaluating.

Please can someone correct my namespace/query syntax.

Thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:52:04+00:00Added an answer on May 18, 2026 at 8:52 am

    I don’t normally use libxml2, I much prefer lxml.etree.

    Had a play around. The xpathEval method on your node creates a new context each time, apparently without the namespace you registered.

    You can reset your context to different locations like this:

    >>> import libxml2
    >>> from urllib2 import urlopen
    >>> data = urlopen('http://pastie.org/pastes/1333021/download').read()
    >>>
    >>> doc = libxml2.parseMemory(data,len(data))
    >>>
    >>> context = doc.xpathNewContext()
    >>> context.xpathRegisterNs("db", "http://examplenamespace.com")
    0
    >>>
    >>> for res in context.xpathEval("//db:food"):
    ...     context.setContextNode(res)
    ...     print "Got Food Node:"
    ...     desc = context.xpathEval('./db:description')[0]
    ...     print desc
    ...
    Got Food Node:
    <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
    Got Food Node:
    <description>light Belgian waffles covered with strawberries and whipped cream</description>
    Got Food Node:
    <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    Got Food Node:
    <description>thick slices made from our homemade sourdough bread</description>
    Got Food Node:
    <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.