Skip to content

Parse GPX files that bind the GPX namespace to a prefix - #311

Open
cpruijsen wants to merge 1 commit into
tkrajina:devfrom
cpruijsen:fix/issue-242-1a05e0f0
Open

cpruijsen wants to merge 1 commit into
tkrajina:devfrom
cpruijsen:fix/issue-242-1a05e0f0

Conversation

@cpruijsen

Copy link
Copy Markdown

Fixes #242.

gpxpy/parser.py now removes the GPX 1.0 and GPX 1.1 namespaces from element tags after it builds the element tree. A document that binds http://www.topografix.com/GPX/1/1 to a prefix then parses like a document that uses it as the default namespace. Polar Access Manager writes files like this:

<ns2:gpx xmlns="http://www.garmin.com/xmlschemas/TrackPointExtension/v2"
         xmlns:ns2="http://www.topografix.com/GPX/1/1" ...>
  <ns2:trk>...</ns2:trk>
</ns2:gpx>

Before this change, a prefixed root failed with GPXException: Document must have a `gpx` root node.. Prefixed children under an unprefixed root parsed with no tracks, because find('trk') does not match {http://www.topografix.com/GPX/1/1}trk.

Elements in other namespaces (extensions) keep their qualified names, so nsmap and the to_xml() round trip do not change. notok.gpx from the issue zip now parses with its track, points, metadata and heart-rate extension. ok.gpx gives the same result as before.

test.py adds test_with_prefixed_gpx_namespace (prefixed root and children, with an extension and a to_xml() round trip) and test_with_prefixed_gpx_elements (unprefixed root, prefixed children). Both fail without the change and pass with it.

One judgement call: Topografix-namespaced elements inside <extensions> also become unqualified. They are GPX elements, and to_xml() writes them under the GPX default namespace. If you prefer to keep extension content as it is, I can limit the change to elements outside <extensions>.

Strip the topografix GPX namespace from element tags after building
the element tree, so documents like <ns2:gpx> parse the same as
documents using the default namespace. Elements in other namespaces
(extensions) keep their qualified names.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot parse files where the default namespace is http://www.garmin.com/xmlschemas/TrackPointExtension/v2

1 participant