Python Forum
Reusing same namespace name when writing back XML with xml.etree.ElementTree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reusing same namespace name when writing back XML with xml.etree.ElementTree
#1
I'm editing an XML file with xml.etree.ElementTree. On input the XML has a namespace defined so all tags are of the form
<somenamespace:tag>
When I write the XML back to file, the namespace is called ns0 and the tags look like:

<ns0:tag>
This is valid XML but not as readable. How can i tell Python to use "somenamespace" instead of "ns0"? I tried ET.register_namespace(prefix,namespace) but that doesn't work.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#2
Oops, actually a problem in my code. ET.register_namespace(prefix,namespace) works provided that:

  1. It executed before reading the XML
  2. The namespace string is the correct one.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which namespace? Skaperen 4 1,256 May-28-2024, 05:38 AM
Last Post: Gribouillis
  xml file editing with lxml.etree FlavioBueno 2 2,110 Jun-09-2023, 02:00 PM
Last Post: FlavioBueno
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 2,472 May-27-2022, 04:59 PM
Last Post: Winfried
  [ElementTree] Insert big block of HTML? Winfried 0 1,672 May-12-2022, 07:08 AM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 3,705 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  'namespace' shorthand for function arguments? shadowphile 5 3,537 Aug-11-2021, 09:02 PM
Last Post: shadowphile
  xml.etree.ElementTree question. water 0 4,008 Oct-09-2020, 06:47 PM
Last Post: water
  [PyKML] Loop through all Placemarks; Remove namespace Winfried 2 4,608 Aug-28-2020, 09:24 AM
Last Post: Winfried
  xml.etree.ElementTree extract string values matthias100 2 7,784 Jul-12-2020, 06:02 PM
Last Post: snippsat
  [pykml] "AttributeError: 'lxml.etree._ElementTree' object has no attribute 'Document' Winfried 3 8,525 May-26-2020, 09:30 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020