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

  • SEARCH
  • Home
  • 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 4238800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:57:04+00:00 2026-05-21T02:57:04+00:00

since I had this annoying issue for the 2nd time, I thought that asking

  • 0

since I had this annoying issue for the 2nd time, I thought that asking would help.

Sometimes I have to get Elements from XML documents, but the ways to do this are awkward.

I’d like to know a python library that does what I want, a elegant way to formulate my XPaths, a way to register the namespaces in prefixes automatically or a hidden preference in the builtin XML implementations or in lxml to strip namespaces completely. Clarification follows unless you already know what I want 🙂

Example-doc:

<root xmlns="http://really-long-namespace.uri"
  xmlns:other="http://with-ambivalent.end/#">
    <other:elem/>
</root>

What I can do

The ElementTree API is the only builtin one (I know of) providing XPath queries. But it requires me to use “UNames.” This looks like so: /{http://really-long-namespace.uri}root/{http://with-ambivalent.end/#}elem

As you can see, these are quite verbose. I can shorten them by doing the following:

default_ns = "http://really-long-namespace.uri"
other_ns   = "http://with-ambivalent.end/#"
doc.find("/{{{0}}}root/{{{1}}}elem".format(default_ns, other_ns))

But this is both {{{ugly}}} and fragile, since http…end/# ≃ http…end# ≃ http…end/ ≃ http…end, and who am I to know which variant will be used?

Also, lxml supports namespace prefixes, but it does neither use the ones in the document, nor provides an automated way to deal with default namespaces. I would still have to get one element of each namespace to retrieve it from the document. Namespace attributes are not preserved, so no way of automatically retrieving them from these, too.

There is a namespace-agnostic way of XPath queries, too, but it is both verbose/ugly and unavailable in the builtin implementation: /*[local-name() = 'root']/*[local-name() = 'elem']

What I want to do

I want to find a library, option or generic XPath-morphing function to achieve above examples by typing little more than the following…

  1. Unnamespaced: /root/elem
  2. Namespace-prefixes from document: /root/other:elem

…plus maybe some statements that i indeed want to use the document’s prefixes or strip the namespaces.

Further clarification: although my current use case is as simple as that, I will have to use more complex ones in the future.

Thanks for reading!


Solved

The user samplebias directed my attention to py-dom-xpath; Exactly what i was looking for. My actual code now looks like this:

#parse the document into a DOM tree
rdf_tree = xml.dom.minidom.parse("install.rdf")
#read the default namespace and prefix from the root node
context = xpath.XPathContext(rdf_tree)

name    = context.findvalue("//em:id", rdf_tree)
version = context.findvalue("//em:version", rdf_tree)

#<Description/> inherits the default RDF namespace
resource_nodes = context.find("//Description/following-sibling::*", rdf_tree)

Consistent with the document, simple, namespace-aware; perfect.

  • 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-21T02:57:05+00:00Added an answer on May 21, 2026 at 2:57 am

    The *[local-name() = "elem"] syntax should work, but to make it easier you can create a function to simplify construction of the partial or full “wildcard namespace” XPath expressions.

    I’m using python-lxml 2.2.4 on Ubuntu 10.04 and the script below works for me. You’ll need to customize the behavior depending on how you want to specify the default namespaces for each element, plus handle any other XPath syntax you want to fold into the expression:

    import lxml.etree
    
    def xpath_ns(tree, expr):
        "Parse a simple expression and prepend namespace wildcards where unspecified."
        qual = lambda n: n if not n or ':' in n else '*[local-name() = "%s"]' % n
        expr = '/'.join(qual(n) for n in expr.split('/'))
        nsmap = dict((k, v) for k, v in tree.nsmap.items() if k)
        return tree.xpath(expr, namespaces=nsmap)
    
    doc = '''<root xmlns="http://really-long-namespace.uri"
        xmlns:other="http://with-ambivalent.end/#">
        <other:elem/>
    </root>'''
    
    tree = lxml.etree.fromstring(doc)
    print xpath_ns(tree, '/root')
    print xpath_ns(tree, '/root/elem')
    print xpath_ns(tree, '/root/other:elem')
    

    Output:

    [<Element {http://really-long-namespace.uri}root at 23099f0>]
    [<Element {http://with-ambivalent.end/#}elem at 2309a48>]
    [<Element {http://with-ambivalent.end/#}elem at 2309a48>]
    

    Update: If you find out you do need to parse XPaths, you can check out projects like py-dom-xpath which is a pure Python implementation of (most of) XPath 1.0. In the least that will give you some idea of the complexity of parsing XPath.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have had this doubt for a long time . I download a project
And this is pretty annoying since we have it set up to continuously check
Hey, Since Google Fonts came out, I have had this question in mind. First
I've had this intermittent issue when using asp.net. My site is dynamically compiled. Sometimes
Its been a while since I had to do some Java/JSP... I have a
Since the early days, Palm OS has had a special easter egg mode that's
Ever since deleting the original settings.settings file I have had endless problems getting the
Since CS3 doesn't have a web service component, as previous versions had, is there
it’s been a while since the last time I had to use regex, I
I had an annoying issue, getting a Possible loss of precision error when compiling

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.