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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:28:47+00:00 2026-05-26T23:28:47+00:00

In python 2.7 (with etree 1.3), I can suppress the XML prefixes on elements

  • 0

In python 2.7 (with etree 1.3), I can suppress the XML prefixes on elements like this:

Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as etree
>>> etree.VERSION
'1.3.0'
>>> something = etree.Element('{http://some.namespace}token')
>>> etree.tostring(something)
'<ns0:token xmlns:ns0="http://some.namespace" />'
>>> etree.register_namespace('', 'http://some.namespace')
>>> etree.tostring(something)
'<token xmlns="http://some.namespace" />'

The register_namespace function was added in 1.3. I’m trying to remove the prefix in a way that is compatible with python 2.6’s etree at version 1.2.6. Here’s what I’ve tried:

Python 2.6.7 (r267:88850, Jul 31 2011, 19:30:54) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as etree
>>> etree.VERSION
'1.2.6'
>>> something = etree.Element('{http://some.namespace}token')
>>> etree.tostring(something)
'<ns0:token xmlns:ns0="http://some.namespace" />'
>>> etree._namespace_map['http://some.namespace'] = ''
>>> etree.tostring(something)
'<:token xmlns:="http://some.namespace" />'

This is not what I want. The prefixes are still there but are blank. Is there any way to remove them completely?

  • 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-26T23:28:47+00:00Added an answer on May 26, 2026 at 11:28 pm

    After looking at the source code for ElementTree in python2.6, the : is hard coded in the fixtag function. As a workaround, here’s what I did:

    from xml.etree import ElementTree as etree
    
    if etree.VERSION[0:3] == '1.2':
        #in etree < 1.3, this is a workaround for supressing prefixes
    
        def fixtag(tag, namespaces):
            import string
            # given a decorated tag (of the form {uri}tag), return prefixed
            # tag and namespace declaration, if any
            if isinstance(tag, etree.QName):
                tag = tag.text
            namespace_uri, tag = string.split(tag[1:], "}", 1)
            prefix = namespaces.get(namespace_uri)
            if namespace_uri not in namespaces:
                prefix = etree._namespace_map.get(namespace_uri)
                if namespace_uri not in etree._namespace_map:
                    prefix = "ns%d" % len(namespaces)
                namespaces[namespace_uri] = prefix
                if prefix == "xml":
                    xmlns = None
                else:
                    if prefix is not None:
                        nsprefix = ':' + prefix
                    else:
                        nsprefix = ''
                    xmlns = ("xmlns%s" % nsprefix, namespace_uri)
            else:
                xmlns = None
            if prefix is not None:
                prefix += ":"
            else:
                prefix = ''
    
            return "%s%s" % (prefix, tag), xmlns
    
        etree.fixtag = fixtag
        etree._namespace_map['http://some.namespace'] = None
    else:
        #For etree > 1.3, use register_namespace function
        etree.register_namespace('', 'http://some.namespace')
    

    In case this post ever gets out of date, the code is maintained here.

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

Sidebar

Related Questions

I have this XML file : http://dl.dropbox.com/u/10773282/2011/perf.xml It has two Class elements as is
(Python 3.2) I'm using etree to parse some XML. To do this, I'm recursively
How would you do this for an instance of xml.etree.cElementTree.Element ? $ python Python
30 jun 2011 -- I am awarding @Pablo for this question, because of his
In my Python app, I have an XML document that I'd like to transform
please consider this code: import xml.etree.ElementTree as ET import urllib XML_response = urllib.urlopen('http://www.navlost.eu/aero/metar/?icao=LWSK&dt0=2011-05-03+12%3A00%3A00&c=1&rt=metar').read() tree
I'm using python 2.6.2's xml.etree.cElementTree to create an xml document: import xml.etree.cElementTree as etree
Are there any XML parsers for Python that can parse file streams? My XML
i'm having some problems to unterstand the python etree library to read a xml
How to clone Element objects in Python xml.etree ? I'm trying to procedurally move

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.