1

I have a python script that simply reads "input.xml" and copies into "output.xml" file. As showed in "output.xml", Python's Xpath generates ns0, ns1 tag. How to avoid these tags without using other xml libraries (eg. lxml)?

Script:

import xml.etree.ElementTree as ET

fileName = "input.xml"

tree = ET.parse(template)
tree.write("output.xml")

Input.xml:

<Car>
     <brand xmlns = "www.car.com" xmlns:brand="www.bmw.com">
          <arg key="name" value="series 3" />
     </brand>
     <market xmlns = "www.ebay.com">
          <arg key="name" value="auto"/>
     </market>
</Car>

output.xml:

<Car xmlns:ns0="www.car.com" xmlns:ns1="www.ebay.com">
     <ns0:brand>
          <ns0:arg key="name" value="series 3" />
     </ns0:brand>
     <ns1:market>
          <ns1:arg key="name" value="auto" />
     </ns1:market>
</Car>
rypel
  • 4,686
  • 2
  • 25
  • 36
Taewan
  • 1,167
  • 4
  • 15
  • 25

1 Answers1

0

I am afraid, that there is no simple solution to this.

There is an issue in Python bug tracker related to that which is not closed for a while.

You could try to follow the solution proposed there, but it does not look very clear.

My recommendation is to reconsider using lxml - it provides real power to XML processing, Google AppEngine is including this.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • Hi Jan, does lxml support python 3.4? – Taewan Jan 09 '15 at 10:53
  • @Taewan Very good question. On my Linux machine I got it installed without problem - source code of lxml claims "This lxml version requires Python 2.6, 2.7, 3.2 or later.". This is using source distribution thus compiling it. On Windows, you may follow this question on SO http://stackoverflow.com/questions/23944465/installing-lxml-for-python-3-4-on-windows-x-86-32-bit-with-visual-studio-c-2 . Anyway, I am using `lxml` for years and highly recommend it - the effort in overcoming possible installation problems will pay back very soon. – Jan Vlcinsky Jan 09 '15 at 11:09