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 8881015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:15:41+00:00 2026-06-14T20:15:41+00:00

I need to write a dynamic function that finds elements on a subtree of

  • 0

I need to write a dynamic function that finds elements on a subtree of an ATOM xml by building dynamically the XPath to the element.

To do so, I’ve written something like this:

    tree = etree.parse(xmlFileUrl)
    e = etree.XPathEvaluator(tree, namespaces={'def':'http://www.w3.org/2005/Atom'})
    entries = e('//def:entry')
    for entry in entries:
        mypath = tree.getpath(entry) + "/category"
        category = e(mypath)

The code above fails to find “category” because getpath() returns an XPath without namespaces, whereas the XPathEvaluator e() requires namespaces.

Although I know I can use the path and provide a namespace in the call to XPathEvaluator, I would like to know if it’s possible to make getpath() return a “fully qualified” path, using all the namespaces, as this is convenient in certain cases.

(This is a spin-off question of my earlier question: Python XpathEvaluator without namespace)

  • 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-06-14T20:15:42+00:00Added an answer on June 14, 2026 at 8:15 pm

    Rather than trying to construct a full path from the root, you can evaluate XPath expression on with the entry as the base node:

    tree = etree.parse(xmlFileUrl)
    nsmap = {'def':'http://www.w3.org/2005/Atom'}
    entries_expr = etree.XPath('//def:entry', namespaces=nsmap)
    category_expr = etree.XPath('category')
    for entry in entries_expr(tree):
        category = category_expr(entry)
    

    If performance is not critical, you can simplify the code by using the .xpath() method on elements rather than pre-compiled expressions:

    tree = etree.parse(xmlFileUrl)
    nsmap = {'def':'http://www.w3.org/2005/Atom'}
    for entry in tree.xpath('//def:entry', namespaces=nsmap):
        category = entry.xpath('category')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a dynamic function that finds elements on a subtree of
I need to write a plugin for Dynamics CRM 4.0 that executes when a
I need to write a C++ code coverage program that takes in another C++
I need to write a program that prints 0.(03) for input 1 and 33.
I need to write a script that does the following: # open a tiff
I have an ASP.NET user control that I want to write some dynamic Javascript
I have a view that contains a javascript function that i need to init/call
I'm trying to write a function that conditionaly prunes out data based on parameters
I have have a a function that when a checkbox is checked i dynamically
I've read on this page that dynamic arrays need ShareMem unit to work properly.

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.