Missing option to change namespace mappings

Bug #555602 reported by Wichert Akkerman on 2010-04-05
38
This bug affects 7 people
Affects Status Importance Assigned to Milestone
lxml
Wishlist
Unassigned

Bug Description

This is essentially a summary of a thread on the mailinglist: http://codespeak.net/pipermail/lxml-dev/2010-March/005329.html

For some XML processing tools I need to be able to change the namespace mapping, specifically to insert a new entry into the namespace map. My test code looked like this:

NS="http://xml.zope.org/namespaces/i18n"
tree=lxml.etree.parse(input)
root=tree.getroot()
count=1
if "i18n" not in root.nsmap:
     root.nsmap["i18n"]=NS
for el in root.iter():
     if "{%s}translate" % NS in el.attrib:
         continue
     if hasText(el):
         el.attrib["{%s}translate" % NS]="string%d" % count
         count+=1
print lxml.etree.tostring(tree)

This did not work since root.nsmap ignores any changes you make in it. An alternative implementation suggested by Simon Wiles created a new root element with the right nsmap and moved all children over, but approach looses data (specifcally the docinfo).

The missing lxml feature to make this possible is a way to change the namespace mapping.

scoder (scoder) on 2010-07-24
Changed in lxml:
importance: Undecided → Wishlist
scoder (scoder) on 2014-02-20
Changed in lxml:
status: New → Confirmed
Bruno Narciso (brunonar) wrote :

This is critical to convert XML-like versions as GPX 1.0 to 1.1.

> This is critical to convert XML-like versions as GPX 1.0 to 1.1.

Oh, I'd certainly take patches that implement this.

Bruno Narciso (brunonar) wrote :

Oh, sorry, the version that I'm using is 3.3.1 (pip install). Just now I saw that there is the version 3.4.
Thanks.

Lennart Regebro (regebro-gmail) wrote :

I think etree.register_namespace() might be the solution to this.

scoder (scoder) wrote :

For a global mapping/redefinition of a prefix, yes, I agree. Not sure if this is meant here.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers