tkrajina / gpxpy

gpx-py is a python GPX parser. GPX (GPS eXchange Format) is an XML based file format for GPS tracks.
Apache License 2.0
987 stars 223 forks source link

to_xml removes metadata extensions without the name tag present #280

Open CyrilSLi opened 2 months ago

CyrilSLi commented 2 months ago

Test code:

import gpxpy
gpx = gpxpy.parse("""\
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="test">
  <metadata>
    <name>Name</name>
    <extensions>
      <tag>value</tag>
    </extensions>
  </metadata>
  <trk>
  </trk>
</gpx>""")
print(gpx.metadata_extensions) # [<Element 'tag' at 0x123456789>]
gpx.name = None
print(gpx.to_xml())
"""
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" version="1.1" creator="test">
  <trk>
  </trk>
</gpx>
"""
gpx = gpxpy.parse(gpx.to_xml())
print(gpx.metadata_extensions) # []
CyrilSLi commented 2 months ago

I am pretty sure that the problem lies within gpx_fields_to_xml in gpxfield.py, with value = getattr(instance, gpx_field.name) being a likely culprit, however I am not familiar with the variables used.