-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
malformed xml #234
Comments
Definitions of nsmap and schema location might help. Try using the following code: import gpxpy
import gpxpy.gpx
fn = "testfile.gpx"
with open(fn) as f:
gf = gpxpy.parse(f)
gf.nsmap['gpxtpx'] = 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1'
gf.nsmap['gpxx'] = 'http://www.garmin.com/xmlschemas/GpxExtensions/v3'
gf.nsmap['gpx_style'] = 'http://www.topografix.com/GPX/gpx_style/0/2'
gf.schema_locations = [
'http://www.topografix.com/GPX/1/1',
'http://www.topografix.com/GPX/1/1/gpx.xsd',
'http://www.garmin.com/xmlschemas/GpxExtensions/v3',
'http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd',
'http://www.garmin.com/xmlschemas/TrackPointExtension/v1',
'http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd',
'http://www.topografix.com/GPX/gpx_style/0/2',
'http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd']
with open(fn+".new.gpx", 'w+') as f:
f.write(gf.to_xml('1.1')) |
Thanks a lot, @ekspla! I just started using this library this week and I noticed that my GPX files went from: <extensions>
<line xmlns="http://www.topografix.com/GPX/gpx_style/0/2">
(omitted)
</line>
</extensions> to <extensions>
<http://www.topografix.com/GPX/gpx_style/0/2:line>
(omitted)
</http://www.topografix.com/GPX/gpx_style/0/2:line>
</extensions> after I loaded it and saved it. Thanks to your comment, I just had to add self._data.nsmap['gpx_style'] = 'http://www.topografix.com/GPX/gpx_style/0/2' before saving and the problem was fixed 🙌 . |
Thanks @fernandobrito . I don't have the time to fix this immediately, but at least I wrote a test. If somebody wants to try, it's here 0f6c473 To run that test:
|
I am writing this comment in the hope of someone's PR fixing most of the namespace issues very soon. I think there are more than one issue related to the namespace (such as #242), and the issues should be solved together by a single PR. The patch I will show below is of adhoc/preliminary version that may introduce another issues/errors, and that is not suitable as PR for review. There are at least two problems in the code related to these namespace issues.
When parsing the given xml (here after, 'test_linestyle.gpx') with gpx_style as shown in #254: def parse() in class GPXParser of parser.py https://github.com/tkrajina/gpxpy/blob/dev/gpxpy/parser.py#L105-#L109 after line 105-107, prefixes and URIs are as followings.
Note that the second one (GPX/1/1) is overwritten by the forth one because of the same prefix (''). at line 108-109 if prefix == '':
prefix = 'defaultns' # alias default for easier handling Now, the namespace without prefix ('') is renamed as 'defaultns' for handling. Finally when the gpx file is parsed, nsmap is as follows:
This is because, as described before, the second xmlns (GPX/1/1) was overwritten by the fourth one (gpx_style). When serializing to xml, def gpx_fields_to_xml() of gpxfield.py at line 501https://github.com/tkrajina/gpxpy/blob/dev/gpxpy/gpxfield.py#L501 namespaces.remove('defaultns') 'defaultns' is removed. So it's now renamed as '' once again. def to_xml() in class GPX of gpx.py at line 2694 https://github.com/tkrajina/gpxpy/blob/dev/gpxpy/gpx.py#L2694 self.nsmap['defaultns'] = 'http://www.topografix.com/GPX/{}'.format(version_path) 'defaultns' is appended. Note that namespace other than GPX/1/1 is not allowed to be the default. In these combination, 'defaultns' is replaced in nsmap from
to
An adhoc/preliminary version of patch is as followings. This patch is not tested and may cause another issues/errors. In parser.py https://github.com/tkrajina/gpxpy/blob/dev/gpxpy/parser.py#L105-L114 def parse(self, version: Optional[str]=None) -> mod_gpx.GPX:
for namespace in mod_re.findall(r'\sxmlns:?[^=]*="[^"]+"', self.xml):
prefix, _, URI = namespace[6:].partition('=')
prefix = prefix.lstrip(':')
if prefix == '':
#prefix = 'defaultns' # alias default for easier handling
if 'defaultns' not in self.gpx.nsmap:
prefix = 'defaultns'
else:
prefix = 'ns'
else:
if prefix.startswith("ns"): Using this patch,
|
When importing some gpx-files with usage of extensions will broke the file.
I use a downloaded gpx file, which worked with all tools. When importing and exporting the file it is broken.
This mainly is caused by a Line-Style-Extension.
with the following example file.
testfile.gpx
The text was updated successfully, but these errors were encountered: