I\'m trying to develop simple Python (3.2) code to read XML files, do some corrections and store them back. However, during the storage step ElementTree adds this n
In order to avoid the ns0
prefix the default namespace should be set before reading the XML data.
ET.register_namespace('', "http://www.topografix.com/GPX/1/1")
ET.register_namespace('', "http://www.topografix.com/GPX/1/0")
If you try to print the root, you will see something like this: http://www.host.domain/path/to/your/xml/namespace}RootTag' at 0x0000000000558DB8>
So, to avoid the ns0 prefix, you have to change the default namespace before parsing the XML data as below:
ET.register_namespace('', "http://www.host.domain/path/to/your/xml/namespace")
It seems that you have to declare your namespace, meaning that you need to change the first line of your xml from:
<ns0:trk>
to something like:
<ns0:trk xmlns:ns0="uri:">
Once did that you will no longer get ParseError: for unbound prefix: ...
, and:
elem.tag = elem.tag[(len('{uri:}'):]
will remove the namespace.
You need to register all your namespaces before you parse xml file.
For example: If you have your input xml like this and Capabilities is the root of your Element tree.
<Capabilities xmlns="http://www.opengis.net/wmts/1.0"
xmlns:ows="http://www.opengis.net/ows/1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd"
version="1.0.0">
Then you have to register all the namespaces i.e attributes present with xmlns
like this:
ET.register_namespace('', "http://www.opengis.net/wmts/1.0")
ET.register_namespace('ows', "http://www.opengis.net/ows/1.1")
ET.register_namespace('xlink', "http://www.w3.org/1999/xlink")
ET.register_namespace('xsi', "http://www.w3.org/2001/XMLSchema-instance")
ET.register_namespace('gml', "http://www.opengis.net/gml")